Reverse Geocoding in Windows Phone 8

This is a support post for the Inside Windows Phone show on location and mapping in Windows Phone 8 published here.

Reverse geocoding is the process of getting an address from a latitude/longitude pair. With Windows Phone, this process is not only easy, it works in conjunction with the offline mapping capability so that developers can query addresses from a geocoordinate even when there is no data signal available.

The process is extremely straightforward: Add the namespace

<span style="background: white; color: blue;">using </span><span style="background: white; color: black;">Microsoft.Phone.Maps.Services;</span>

And create a new ReverseGeocodeQuery, give it a valid GeoCoordinate and set up the an event handler to manage the result.

<span style="background: white; color: rgb(43, 145, 175);">ReverseGeocodeQuery </span><span style="background: white; color: black;">reverseGeocode = </span><span style="background: white; color: blue;">new </span><span style="background: white; color: rgb(43, 145, 175);">ReverseGeocodeQuery</span><span style="background: white; color: black;">();
</span><span style="background: white; color: black;">reverseGeocode.GeoCoordinate = </span><span style="background: white; color: blue;">new </span><span style="background: white; color: rgb(43, 145, 175);">GeoCoordinate</span><span style="background: white; color: black;">(47.60887, -122.34094);
reverseGeocode.QueryCompleted += reverseGeocode_QueryCompleted;
reverseGeocode.QueryAsync();</span>

Then in our event handler we will get a result (or list of possible results) back as a MapAddress object.

<span style="background: white; color: blue;">void </span><span style="background: white; color: black;">reverseGeocode_QueryCompleted(</span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">sender, </span><span style="background: white; color: rgb(43, 145, 175);">QueryCompletedEventArgs</span><span style="background: white; color: black;">&lt;</span><span style="background: white; color: rgb(43, 145, 175);">IList</span><span style="background: white; color: black;">&lt;</span><span style="background: white; color: rgb(43, 145, 175);">MapLocation</span><span style="background: white; color: black;">&gt;&gt; e)
{
    </span><span style="background: white; color: rgb(43, 145, 175);">MapAddress </span><span style="background: white; color: black;">geoAddress = e.Result[0].Information.Address;           
}</span>

This object will contain a nice helpful set of properties that we can then use to construct a valid address.

image 

One thought on “Reverse Geocoding in Windows Phone 8

Comments are closed.