calculate straight line distance

frizzello
12-13-2006, 03:36 AM
Hi to all,

i'd like to know a method to calculate straight line distance between two points.

To add a line i do

map.Shapes.addLine(location1,location2)

and distance?

Thank you for any suggestion,

Francesco

davidb
12-13-2006, 07:53 AM
Think this works in VB6 when you haven't calculated a route

oMap.Shapes.AddLine location1, location2
Distance# = oMap.Distance(location1, location2)

There's a good example on MapPoint help under 'Distance Method' using both VB6 and C#

Rgds, David

Wilfried
12-14-2006, 12:58 PM
Hi Francesco,

In addition to the reply of David, if you have to do it with only latitude / longitude you can use this formula:

public static int CalcDistance(double lat1, double lon1, double lat2, double lon2, out int heading)
{
double lat = lat2 - lat1;
double lon = (lon2 - lon1) * Math.Cos(Tools.deg2rad((int)((lat1 + lat2) / 2)));
double dist = Math.Sqrt(lat * lat + lon * lon);

heading = Tools.rad2deg(Math.Acos(lat / dist));
if (lon < 0) // Western direction
heading = 360 - heading;

return (int)(dist * 60 * 1852);
}

private static double deg2rad(int degree)
{
return degree * Math.PI / 180;
}

private static int rad2deg(double rad)
{
return (int)(rad * 180 / Math.PI);
}

 
Web mp2kmag.com
mapforums.com