nodejs--根据坐标算距离

var EARTH_RADIUS = 6378.137; //地球半径  
//将用角度表示的角转换为近似相等的用弧度表示的角 java Math.toRadians  
function rad(d) {
    return d * Math.PI / 180.0;
}
/** 
 * 谷歌地图计算两个坐标点的距离 
 * @param lng1  经度1 
 * @param lat1  纬度1 
 * @param lng2  经度2 
 * @param lat2  纬度2 
 * @return 距离(千米) 
 */
function getDistance(lng1, lat1, lng2, lat2) {
    var radLat1 = rad(lat1);
    var radLat2 = rad(lat2);
    var a = radLat1 - radLat2;
    var b = rad(lng1) - rad(lng2);
    var 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 = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
}


// let a = distance([119.9969915847, 30.2754597274,], [119.9972383479, 30.2759230036])
// let a = distance([30.2822920169, 125.0036899474], [30.2818472718, 120.0034646419])
let a = getDistance(30.27895275, 119.9921260576, 30.2832692396, 120.0249984587)
console.log(a)

 

你可能感兴趣的:(nodejs)