获取两个点经纬度之间的距离

/**
 * js获取两个经纬度之间的距离
 * @param lat1 第一点的纬度
 * @param lng1 第一点的经度
 * @param lat2 第二点的纬度
 * @param lng2 第二点的经度
 * @returns {Number}
 */

function getDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    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 *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;   // 单位为km
}

腾讯地图获取两个坐标之间的距离

// 引入SDK核心类
var QQMapWX = require('xxx/qqmap-wx.js');
 
// 实例化API核心类
var demo = new QQMapWX({
    key: '开发密钥(key)' // 必填
});
 
// 调用接口
demo.calculateDistance({
    to:[{
        latitude: 39.984060,
        longitude: 116.307520
    }, {
        latitude: 39.984572,
        longitude: 116.306339
    }],
    success: function(res) {
        console.log(res);
    },
    fail: function(res) {
        console.log(res);
    },
    complete: function(res) {
        console.log(res);
    }
});

猜你喜欢

转载自www.cnblogs.com/kimigo168/p/9124016.html