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;"><</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;">="myMap" </span><span style="background: white; color: red;">Tap</span><span style="background: white; color: blue;">="ReadMapTap" /></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);">"It is " </span><span style="background: white; color: black;">+ distanceToMSFT.ToString() + </span><span style="background: white; color: rgb(163, 21, 21);">" meters from there to Microsoft!"</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!)