ba272
asked on
Calculating distance from two coordinates
Hi,
Has anybody got a formula to estimate distance (as the crow flies) between two sets of lat/longs? I live in Boston and know that there is a slight loss of precision, even at this latitude, with the assumption that distance is the same in for both the north and the east directions, but it will be precise enough for me.
Thanks,
Bob
Has anybody got a formula to estimate distance (as the crow flies) between two sets of lat/longs? I live in Boston and know that there is a slight loss of precision, even at this latitude, with the assumption that distance is the same in for both the north and the east directions, but it will be precise enough for me.
Thanks,
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a bunch.
Bob
Bob
ASKER
Greg,
What are the units for the double returned by CalculateHaversineDistance ()?
Thanks,
Bob
What are the units for the double returned by CalculateHaversineDistance
Thanks,
Bob
http://mathforum.org/library/drmath/view/51722.html
With this formula, it is by easy to write the C# code for calculating the distance:
// Define our two points
Point p1 = new Point(73, 40); // 73 degrees west 40 degrees north (New York City)
Point p2 = new Point(-6, 50); // 6 degrees east and 50 degrees north (Cologne)
double DegToRadFactor = (Math.PI / 180);
double RadiusOfTheEarth = 3959; /// Radius of the earth in miles (can also be 6371 kilometers)
/// Math.Cos just use radiants, instead of degrees, so calculate the rad's
double p1X = p1.X * DegToRadFactor;
double p1Y = p1.Y * DegToRadFactor;
double p2X = p2.X * DegToRadFactor;
double p2Y = p2.Y * DegToRadFactor;
double der = Math.Cos(p1Y) * Math.Cos(p2Y) * Math.Cos(p2X - p1X) + Math.Sin(p1Y) * Math.Sin(p2Y);
/// Calculate Distance
double distance = Math.Acos(der) * RadiusOfTheEarth;