Thursday 12 November 2009

Bing Maps and Windows Mobile Part 5

A question was recently asked on the Smart Device MSDN forums here. This regarded getting an address from latitude and longitude co-ordinates.

In an attempt to answer the question I had a quick look at the Bing Maps Web Service and found that ReverseGeocode method was available and posted a link to the relevant MSDN documentation here.

As I had already blogged about Bing Maps and Windows Mobile, I thought I would check what the method returned.

I used the Geocode method to return the Latitude and Longitude from my home address and then feed these two values back into the ReverseGeocode method and was returned an address string, although the house number was 15 numbers out.

The method I used is below, please note that the LatitudeSpecified and LongitudeSpecified had to set to ensure that a valid response was receieved.

private string ReverseGeocodeCoordinates(double latitude, double longitude)
{
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
reverseGeocodeRequest.Credentials = new WindowsMobileBingMapsTester.net.virtualearth.dev.staging.Credentials();
reverseGeocodeRequest.Credentials.Token = GetClientToken();

reverseGeocodeRequest.Location = new WindowsMobileBingMapsTester.net.virtualearth.dev.staging.Location();
reverseGeocodeRequest.Location.Latitude = latitude;
reverseGeocodeRequest.Location.LatitudeSpecified = true;
reverseGeocodeRequest.Location.Longitude = longitude;
reverseGeocodeRequest.Location.LongitudeSpecified = true;

// Make the geocode request
GeocodeService geocodeService = new GeocodeService();
GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

if (geocodeResponse.Results.Length > 0)
if (geocodeResponse.Results[0].Locations.Length > 0)
return geocodeResponse.Results[0].DisplayName;

return null;
}

Usage :-

string currentAddress = ReverseGeocodeCoordinates(47.608, -122.337);

Hopefully this will help those looking to determine an address from a set of co-ordinates.

2 comments:

  1. Hi, can you please post the entire project?. I want to implement this but I'm having trouble. Thanks.

    ReplyDelete
  2. Yeah i would like this posted too, cant get any of it to work. There's too many errors.
    Thanks

    ReplyDelete