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

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

问题描述

我计算两个地理坐标之间的距离。我对其他应用程序3-4测试我的应用程序。当我计算距离,我倾向于而其他应用越来越3.5英里能为我的计算,平均3.3英里。这对计算我试图执行一个很大的区别。有没有什么好的类库在那里计算距离?我计算它像这样在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 sLatitudeRadians = sLatitude * (Math.PI / 180.0);
    var sLongitudeRadians = sLongitude * (Math.PI / 180.0);
    var eLatitudeRadians = eLatitude * (Math.PI / 180.0);
    var eLongitudeRadians = eLongitude * (Math.PI / 180.0);

    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?

推荐答案

会有地理座标类(.NET框架4及更高版本)已经有<一个href=\"http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.getdistanceto.aspx\"相对=nofollow> 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.

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

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