Simple Reverse Geocode with Mouse click

I’ve had a few requests for a simple example of how to implement the Bing Map web services with the Silverlight control. In particular how to call the Reverse Geocoding services on mouse click. This post will show a straight forward and simple way of doing this. Note that most of the code in this post is derived from the following article: http://msdn.microsoft.com/en-us/library/cc879136.aspx I recommend using this article for simple ways to add the other Bing Map web services.

The following steps will be needed to accomplish this task:

1) Create a Silverlight Application in Visual Studios.
2) Add a service reference to the Geocoding service: http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl
3) Add a references to the Silverlight map control.
4) Add the reference in the MainPage.xaml file and add a map object.
5) Add in a reference to your Bing Maps Application Key. I prefer to add this to the map from code behind when the page loads.
6) Add a mouse click event handler to the map and make a call to the Reverse Geocoding service.
7) Display results of Reverse Geocoding service

Below is the code used in the MainPage.xaml.cs file:

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Core;
using BingMapsReverseGeocoding.GeocodeService;

namespace BingMapsReverseGeocoding
{
    public partial class MainPage : UserControl
    {
        private string ApplicationKey = "Your Application Key";

        public MainPage()
        {
            InitializeComponent();
            MyMap.Loaded += new RoutedEventHandler(MainMap_Loaded);
        }

        private void MainMap_Loaded(object sender, RoutedEventArgs e)
        {
            ApplicationIdCredentialsProvider appId = new ApplicationIdCredentialsProvider(ApplicationKey);
            MyMap.CredentialsProvider = appId;

            MyMap.MouseClick += new EventHandler<MapMouseEventArgs>(MyMap_MouseClick);
        }

        private void MyMap_MouseClick(object sender, MapMouseEventArgs e)
        {
            Location location;
            if (MyMap.TryViewportPointToLocation(e.ViewportPoint, out location))
            {
                LatLongLabel.Text = String.Format("Latitude: {0:#.######}, Longitude: {0:#.######}",
                    location.Latitude, location.Longitude);

                ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

                // Set the credentials using a valid Bing Maps key
                reverseGeocodeRequest.Credentials = new Credentials();
                reverseGeocodeRequest.Credentials.ApplicationId = ApplicationKey;

                reverseGeocodeRequest.Location = location;

                // Make the reverse geocode request
                GeocodeServiceClient geocodeService =
                    new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                geocodeService.ReverseGeocodeCompleted +=
                    new EventHandler<ReverseGeocodeCompletedEventArgs>(geocodeService_ReverseGeocodeCompleted);
                geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
            }
        }

        private void geocodeService_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
        {
            // The result is a GeocodeResponse object
            GeocodeResponse geocodeResponse = e.Result;
            ResultViewer.Children.Clear();

            if (geocodeResponse.Results.Count > 0)
            {
                for (int i = 0; i < geocodeResponse.Results.Count; i++)
                {
                    TextBlock result = new TextBlock();
                    result.Text = geocodeResponse.Results[i].DisplayName;
                    ResultViewer.Children.Add(result);
                }
            }
        }
    }
}

Below is the code used in the MainPage.xaml file:

MainPage.xaml

<UserControl x:Class="BingMapsReverseGeocoding.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <m:Map Name="MyMap" Center="40.71, -74" ZoomLevel="17"></m:Map>
        <!– Results Panel –>
        <ScrollViewer Foreground="White">
            <StackPanel Width="400"
                Background="Black" Opacity="0.8"
                HorizontalAlignment="Right"
                VerticalAlignment="Top">
                <TextBlock Name="LatLongLabel"></TextBlock>
                <StackPanel Name="ResultViewer" Width="400">
                </StackPanel>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</UserControl>

 

A complete working source code can be found here:
http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/BingMapsReverseGeocoding.zip

Advertisements

3 thoughts on “Simple Reverse Geocode with Mouse click

    • I haven’t used Python and the Bing Maps SOAP services. Personally I haven’t used the SOAP services in years. The Bing Maps REST services are much better and faster.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s