以km为单位计算两个地理点的距离c# [英] Calculate distance of two geo points in km c#

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

问题描述

我想计算两个地理点的距离.这些点以经度和纬度给出.

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/scripts/latlong.html

这里是我从这个链接使用的代码:在谷歌地图 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.

我的结果:163,307 公里

my result: 163,307 km

网站结果:136公里

有什么建议???

托蒂

推荐答案

你的公式几乎正确,但你必须交换经度和纬度的参数

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

我使用的是简化公式:

// 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;
}

测试:

(赤道距离):经度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 km

(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. At web site you use for result comparison, for every point first text box is latitude, second - longitude

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

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