计算公里的C#两个地理点的距离 [英] Calculate distance of two geo points in km c#

查看:271
本文介绍了计算公里的C#两个地理点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I`d像计算两个地理点的距离。该点给出经度和纬度

I`d like calculate the distance of two geo points. the points are given in longitude and latitude.

坐标:

1点:36.578581,-118.291994

point 1: 36.578581, -118.291994

2点:36.23998,-116.83171

point 2: 36.23998, -116.83171

下面一个网站来比较的结果:

here a website to compare the results:

http://www.movable-type.co.uk/

我从这个链接中使用的代码在这里问题/ 1502590 /计算距离之间-两点合谷歌-地图-V3>计算两点之间的谷歌距离映射V3

here the code I used from this link: Calculate distance between two points in google maps V3

    const double PIx = Math.PI;
    const double RADIO = 6378.16;

    /// <summary>
    /// Convert degrees to Radians
    /// </summary>
    /// <param name="x">Degrees</param>
    /// <returns>The equivalent in radians</returns>
    public static double Radians(double x)
    {
        return x * PIx / 180;
    }

    /// <summary>
    /// Calculate the distance between two places.
    /// </summary>
    /// <param name="lon1"></param>
    /// <param name="lat1"></param>
    /// <param name="lon2"></param>
    /// <param name="lat2"></param>
    /// <returns></returns>
    public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
    {
        double R = 6371; // km
        double dLat = Radians(lat2 - lat1);
        double dLon = Radians(lon2 - lon1);
        lat1 = Radians(lat1);
        lat2 = Radians(lat2);

        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        double d = R * c;

        return d;
    }


Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(36.578581, -118.291994, 36.23998, -116.83171));

的问题是,我得到两个不同的结果。

the issue is that I get two different results.

我的结果:该网站的163307公里

my result: 163,307 km

结果:1​​36公里

有什么建议? ?

TORTI

推荐答案

您的公式几乎是正确的,但你有交换参数经度纬度的

Your formula is almost correct, but you have to swap parameters for longitude an latitude

Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(-118.291994, 36.578581, -116.83171, 36.23998)); // = 136 km



我使用的是简单的公式:

I'm using simplified formula:

// cos(d) = sin(φА)·sin(φB) + cos(φА)·cos(φB)·cos(λА − λB),
//  where φА, φB are latitudes and λА, λB are longitudes
// Distance = d * R
public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
{
    double R = 6371; // km

    double sLat1 = Math.Sin(Radians(lat1));
    double sLat2 = Math.Sin(Radians(lat2));
    double cLat1 = Math.Cos(Radians(lat1));
    double cLat2 = Math.Cos(Radians(lat2));
    double cLon = Math.Cos(Radians(lon1) - Radians(lon2));

    double cosD = sLat1*sLat2 + cLat1*cLat2*cLon;

    double d = Math.Acos(cosD);

    double dist = R * d;

    return dist;
}



测试:

Testing:

(在赤道的距离):经度0,100;纬度= 0,0; DistanceBetweenPlaces(0,0,100,0)=11119.5公里

(Distance at Equator): Longitudes 0, 100; Latitudes = 0,0; DistanceBetweenPlaces(0, 0, 100, 0) = 11119.5 km

(在北极的距离):经度0,100;纬度= 90,90; DistanceBetweenPlaces(0,90,100,90)=0公里

(Distance at North Pole): Longitudes 0, 100; Latitudes = 90,90; DistanceBetweenPlaces(0, 90, 100, 90) = 0 km

经度:-118.291994,-116.83171;纬度:36.578581,36.23998 =135.6公里

Longitudes: -118.291994, -116.83171; Latitudes: 36.578581, 36.23998 = 135.6 km

经度:36.578581,36.23998;纬度:-118.291994,-116.83171 =163.2公里

Longitudes: 36.578581, 36.23998; Latitudes: -118.291994, -116.83171 = 163.2 km

最好的问候

P.S。在您使用的结果比较,每个点第一个文本框是纬度,网站第二 - 经度

P.S. At web site you use for result comparison, for every point first text box is latitude, second - longitude

这篇关于计算公里的C#两个地理点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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