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: 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:
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:


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()
            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);

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

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

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


<UserControl x:Class="BingMapsReverseGeocoding.MainPage"
    xmlns:d="" xmlns:mc=""
    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"
                <TextBlock Name="LatLongLabel"></TextBlock>
                <StackPanel Name="ResultViewer" Width="400">


A complete working source code can be found here:

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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s