我正在计算两个地理坐标之间的距离。我正在针对 3-4 个其他应用程序测试我的应用程序。当我计算距离时,我的计算结果往往平均为 3.3 英里,而其他应用程序的计算结果为 3.5 英里。这与我要执行的计算有很大的不同。有什么好的类库可以计算距离吗?我在 C# 中这样计算:
public static double Calculate(double sLatitude,double sLongitude, double eLatitude,
double eLongitude)
{
var radiansOverDegrees = (Math.PI / 180.0);
var sLatitudeRadians = sLatitude * radiansOverDegrees;
var sLongitudeRadians = sLongitude * radiansOverDegrees;
var eLatitudeRadians = eLatitude * radiansOverDegrees;
var eLongitudeRadians = eLongitude * radiansOverDegrees;
var dLongitude = eLongitudeRadians - sLongitudeRadians;
var dLatitude = eLatitudeRadians - sLatitudeRadians;
var result1 = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
Math.Cos(sLatitudeRadians) * Math.Cos(eLatitudeRadians) *
Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
// Using 3956 as the number of miles around the earth
var result2 = 3956.0 * 2.0 *
Math.Atan2(Math.Sqrt(result1), Math.Sqrt(1.0 - result1));
return result2;
}
我做错了什么?我应该先以公里为单位计算,然后再转换为英里吗?
最佳答案
GeoCoordinate类(.NET Framework 4 及更高版本)已经有 GetDistanceTo
方法。
var sCoord = new GeoCoordinate(sLatitude, sLongitude);
var eCoord = new GeoCoordinate(eLatitude, eLongitude);
return sCoord.GetDistanceTo(eCoord);
距离以米为单位。
您需要引用 System.Device。
关于c# - 计算两个纬度和经度坐标之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366408/