Here is a small C# method for calculating distances between two longitude/latitude points based on Movable Type's article on the subject. Since the earth isn't perfectly round, and since I've found several values for the approximate radius of the earth, this method is only approximate, but should be sufficiently accurate for most applications.
这里是一个小的基于 Movable Type's article的C#方法,计算两条经线/纬线上点的距离.因为地球并不完全是圆的,而且也因为我已经找到了好几个地球半径的值,所以这种方法只是近似的,但是,对大多数应用场合,这种方法已经很精确了.
/// <summary>
/// Calculates the distance between to lat/long points and returns the approximate distance in kilometers
/// </summary>
/// <param name="from">Point in long/lat decimal degrees</param>
/// <param name="to">Point in long/lat decimal degrees</param>
/// <returns>Distance in kilometers</returns>
private double CalcDistance(Point from, Point to)
{
double rad = 6371; //Earth radius in Km
//Convert to radians
double p1X = from.X / 180 * Math.PI;
double p1Y = from.Y / 180 * Math.PI;
double p2X = to.X / 180 * Math.PI;
double p2Y = to.Y / 180 * Math.PI;
return Math.Acos(Math.Sin(p1Y) * Math.Sin(p2Y) +
Math.Cos(p1Y) * Math.Cos(p2Y) * Math.Cos(p2X - p1X)) * rad;
}
If you want the distance returned in something else than kilometers, change the radius to whatever unit you would like the output unit to be in.
如果你想使返回的距离以千米为单位,把半径的单位变成你想要的输出单位.
I have successfully incorporated this in a SharpMap-based application for doing nearest-object searches in a map. It is fun to see how the earth-curvate is taken into account; -this is particularily visible towards the poles in a flat projected map.
我已经成功地将它整合到一个基于SharpMap的应用程序当中,它主要是在地图上寻找最近的物体.看到把地球的区度考虑进去是非常有趣的,尤其是和平面地图相比,两极的差别是非常明显的.