Share via


Reverse Geocoding in Windows 8

Wouldn’t it be cool if you could click on a point on the map and get the complete address of that point? Here are the easy steps for including this feature in your Windows 8 App.

1. Add Service Reference:

Right click on Reference folder in your project – select Add Service Reference – Add the below address and click Go – Change namespace to GeocodeService.

https://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl

image

 

2. Now to your C# file add the below reference.

using NameOfYourProject.GeocodeService;

using Bing.Maps;

3. Add references for Maps:

- Change the Solution Platforms to x86

- Right click References – add Reference – select windows – Extensions on the left panel – choose Bing Maps for C#, C++ or Visual Basic and Microsoft Visual C++ Runtime Package – add.

4. Design

- Drap drop Map control onto your designer and name it myMap

- Add textblock to display address and name it Address

- Add Tapped event to myMap

5. C# code

private async void myMap_Tapped(object sender, TappedRoutedEventArgs e)
{

//get point of Tap and convert to location which has latitude and longitude
    var point = e.GetPosition(myMap);
    Bing.Maps.Location loc;
    myMap.TryPixelToLocation(point, out loc);

// Declare ReverseGeocode Request – This is the service that will return address for a particular point.

ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

    // Set the credentials using a valid Bing Maps key – Replace the text in green with your credentials
    reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
    reverseGeocodeRequest.Credentials.ApplicationId = "Map Credentials";

    // Set the point to use to find a matching address
    GeocodeService.Location point1 = new GeocodeService.Location();
    point1.Latitude = loc.Latitude;
    point1.Longitude = loc.Longitude;

    reverseGeocodeRequest.Location = point1;

    // Make the reverse geocode request
    GeocodeServiceClient geocodeService = new GeocodeServiceClient(GeocodeServiceClient.EndpointConfiguration.BasicHttpBinding_IGeocodeService);
    var geocodeResponse = await geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
    if (geocodeResponse.Results.Count > 0)
        Address.Text = geocodeResponse.Results[0].DisplayName;
    else
        Address.Text = "No Results found";

}

In the MainPage constructor add the following lines to add credentials to the map and to set its view.

myMap.Credentials = "Map Credentials";

// replace your credentials with the text in green.

myMap.SetView(new Bing.Maps.Location { Latitude = 25.121381, Longitude = 55.200308 }, 13);

// the current focus point is Dubai. You can change this by changing the Latitude and Longitude in green. 13 is the zoom level.

myMap.MapType = MapType.Birdseye;

// the map types available are birdseye,road and aerial

And that's it.. Run the app!

image

There are many more things you can do with maps like giving the address and getting the latitude and longitude or Searching on the map, etc. Check out the reference for further details.

References:

1. https://msdn.microsoft.com/en-us/library/dd221354.aspx

2. The Demo Project

3. https://msdn.microsoft.com/en-us/library/ff428642.aspx - to get Bing Map key

MapBlog.zip

Comments

  • Anonymous
    May 27, 2013
    You should be using the bing maps REST Services and not the old legacy Soap services.

  • Anonymous
    July 14, 2013
    We should be using REST Services, except as noted here (see #1) the only Microsoft Approved way for non-employees (e.g. non-appex) to perform a basic "Business Search" is to use SOAP services or AJAX web control: #1 msdn.microsoft.com/.../ff428643.aspx The ironic humor is found in the Windows 8 Map App. We find that Microsoft (appex) employees get to perform Business Search (e.g. Fiddler trace a search of "Italian restaurant" with Search Charm) using a REST based api returning JSON. But, they have a secret key and id, and use undocumented api calls. Us lowly developers as well as enterprise Bing customers do not get access to the secret Microsoft REST to JSON for Business Search api currently found in Windows 8. LOL Now, I wonder why developers are not flocking to make Windows 8 applications? Any guesses? ROFL