欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

计算两个经纬度点的距离

程序员文章站 2024-02-14 23:03:46
...
    /// <summary>
    /// 经纬度计算距离
    /// </summary>
    /// <param name="x1"></param>
    /// <param name="y1"></param>
    /// <param name="x2"></param>
    /// <param name="y2"></param>
    /// <returns></returns>
    public static double getLngLatDistance(double x1, double y1, double x2, double y2)
    {
        double pidiv180 = (double)Math.PI / 180;
        double dLat1InRad = y1 * pidiv180;
        double dLong1InRad = x1* pidiv180;
        double dLat2InRad = y2 * pidiv180;
        double dLong2InRad = x2 * pidiv180;
        double dLongitude = dLong2InRad - dLong1InRad;
        double dLatitude = dLat2InRad - dLat1InRad;
        double a = (double)(Math.pow(Math.sin(dLatitude / 2), 2) +
                Math.cos(dLat1InRad) * Math.cos(dLat2InRad) * Math.pow(Math.sin(dLongitude / 2), 2));
        double c = (double)(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
        double dDistance = 6378137 * c;
        return dDistance;
    }

 

相关标签: Java