c# - 计算两个纬度和经度坐标之间的距离

标签 c# windows-phone-7 geocoding latitude-longitude

我正在计算两个地理坐标之间的距离。我正在针对 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/

相关文章:

c# - 无法跟踪实体类型 'X' 的实例,因为已跟踪键值为 '{Id}' 的另一个实例(嵌套对象)

windows-phone-7 - 在windows phone中禁用scrollviewer的垂直滚动行为

Django:如何在数据库中存储地理点

c# - 绑定(bind)/绑定(bind)两个 LongListSelectors 的滚动位置

google-maps - 谷歌地理编码中视口(viewport)和边界之间的区别

angular - 使用 Angular 进行反向地理编码

c# - 如何在 Web Api 中使用 Api key 使用表单例份验证进行服务身份验证

c# - Dapper 和 Npgsql 通过使用带有 IN 子句和 List<int> 的 EXPLAIN 抛出异常

c# - 如何为异步方法创建通用扩展方法?

c# - Windows Phone 8 应用程序错误 "No XAML Was Found at the location..."