微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算两个地理点的距离,单位为km c#

我想要计算两个地理点的距离.点在经度和纬度上给出.

坐标为:

点1:36.578581,-118.291994

point 2:36.23998,-116.83171

这里有一个比较网站的结果:

http://www.movable-type.co.uk/scripts/latlong.html

这里是我从这个链接使用的代码
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));

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

我的结果:163,307公里

网站结果:136公里

有什么建议么???

TORTI

解决方法

你的公式几乎是正确的,但你必须交换经度的参数为纬度
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 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,100,0)= 11119.5公里

(北极距离):长0,100;纬度= 90,90; distanceBetweenPlaces(0,90,90)= 0 km

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

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

最好的祝福

附:在web site,您可以使用结果比较,每个点第一个文本框是纬度,第二个经度

原文地址:https://www.jb51.cc/csharp/94607.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐