The Location class has the DistanceTo method that calculates the distance to a given location. Keep in mind that the distance calculated using this method is not the same as the driving distance; this method only gives you an along-the-straight-line-distance (or “as the crow flies”) between the two locations.

The DistanceTo method takes another location as an argument; the following code shows how to calculate distance between two locations:

//Get the from location
MapPoint.Location fromLocation
= this.findResults.get_Item(ref fromIndex) as MapPoint.Location;
//Get the to location
MapPoint.Location toLocation
= poiResults.get_Item(ref poiIndex) as MapPoint.Location;
//Calculate the distance between the from location and
//to location using the DistanceTo method
double distance = fromLocation.DistanceTo(toLocation);

Another way of calculating the distance between two locations is to call the Distance method on active Map object as shown below:

double distance = app.ActiveMap.Distance(startLocation, endLocation);

Both startLocation and endLocation are of MapPoint.Location type objects.

To learn more about this topic and other related topics, please buy Programming MapPoint in .NET

0 comments