当前位置:网站首页>JS map two-point distance calculation

JS map two-point distance calculation

2022-07-18 18:58:00 Stavin Li

/** *  Obtain the distance between two points through longitude and latitude  * @param lat1lng1 * @param lat2lng2 * @returns  Distance units  km */
const getDistance = function (lat1, lng1, lat2, lng2) {
    
    const radLat1 = (lat1 * Math.PI) / 180.0;
    const radLat2 = (lat2 * Math.PI) / 180.0;
    const a = radLat1 - radLat2;
    const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
    let s =
        2 *
        Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                    Math.cos(radLat1) *
                        Math.cos(radLat2) *
                        Math.pow(Math.sin(b / 2), 2)
            )
        );
    s *= 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 100) / 100;
    return s; //  call  return Distance in km
};
原网站

版权声明
本文为[Stavin Li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/199/202207161505072083.html