计算两个纬度和经度地理坐标之间的距离 [英] Calculating Distance between two Latitude and Longitude GeoCoordinates

查看:44
本文介绍了计算两个纬度和经度地理坐标之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算两个地理坐标之间的距离.我正在针对 3-4 个其他应用程序测试我的应用程序.当我计算距离时,我的计算结果平均为 3.3 英里,而其他应用程序则为 3.5 英里.对于我要执行的计算来说,这是一个很大的不同.是否有用于计算距离的好的类库?我在 C# 中是这样计算的:

I'm calculating the distance between two GeoCoordinates. I'm testing my app against 3-4 other apps. When I'm calculating distance, I tend to get an average of 3.3 miles for my calculation whereas other apps are getting 3.5 miles. It's a big difference for the calculation I'm trying to perform. Are there any good class libraries out there for calculating distance? I'm calculating it like this in 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;
}

我可能做错了什么?我应该先以公里计算,然后再转换为英里吗?

What could I be doing wrong? Should I calculate it in km first and then convert to miles?

推荐答案

GeoCoordinate 类(.NET Framework 4 及更高版本)已经具有 GetDistanceTo 方法.

The GeoCoordinate class (.NET Framework 4 and higher) already has GetDistanceTo method.

var sCoord = new GeoCoordinate(sLatitude, sLongitude);
var eCoord = new GeoCoordinate(eLatitude, eLongitude);

return sCoord.GetDistanceTo(eCoord);

距离以米为单位.

您需要引用 System.Device.

You need to reference System.Device.

这篇关于计算两个纬度和经度地理坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆