Skip to content

Calculating Distance From 2 GeoCoordinates 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.

Calculating the distance between two GeoCoordinates in Windows Phone 8 is about as simple as it can get. First, get a GeoCoordinate. One handy way of doing this is to get the location of a tap on the Map control.

Set up the tap event hander on your Map:

<span style="background: white; color: blue;">&lt;</span><span style="background: white; color: rgb(163, 21, 21);">maps</span><span style="background: white; color: blue;">:</span><span style="background: white; color: rgb(163, 21, 21);">Map </span><span style="background: white; color: red;">x</span><span style="background: white; color: blue;">:</span><span style="background: white; color: red;">Name</span><span style="background: white; color: blue;">=&quot;myMap&quot; </span><span style="background: white; color: red;">Tap</span><span style="background: white; color: blue;">=&quot;ReadMapTap&quot; /&gt;</span>

And then translate the tap location into a GeoCoordinate like so:

<span style="background: white; color: blue;">void </span><span style="background: white; color: black;">ReadMapTap(</span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">sender, System.Windows.Input.</span><span style="background: white; color: rgb(43, 145, 175);">GestureEventArgs </span><span style="background: white; color: black;">e)
{
    </span><span style="background: white; color: rgb(43, 145, 175);">GeoCoordinate </span><span style="background: white; color: black;">tapLocation = <br />         </span><span style="background: white; color: black;">distanceMap.ConvertViewportPointToGeoCoordinate(e.GetPosition((</span><span style="background: white; color: rgb(43, 145, 175);">UIElement</span><span style="background: white; color: black;">)sender));
}</span>

Then we just create another GeoCoordinate, like maybe Microsoft campus:

<span style="background: white; color: rgb(43, 145, 175);">GeoCoordinate </span><span style="background: white; color: black;">Msft = </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.6396, -122.1300);</span>

And just ask one of our GeoCoordinates how far it is to the other one.

<span style="background: white; color: blue;">double </span><span style="background: white; color: black;">distanceToMSFT = tapLocation.GetDistanceTo(Msft);
</span><span style="background: white; color: rgb(43, 145, 175);">MessageBox</span><span style="background: white; color: black;">.Show(</span><span style="background: white; color: rgb(163, 21, 21);">&quot;It is &quot; </span><span style="background: white; color: black;">+ distanceToMSFT.ToString() + </span><span style="background: white; color: rgb(163, 21, 21);">&quot; meters from there to Microsoft!&quot;</span><span style="background: white; color: black;">);
</span>

The result will be in meters and is based on the haversine formula for calculating distance over the surface of the earth (my favorite!)