SOURCE

// 根据经纬度计算距离,参数分别为第一点的纬度,经度;第二点的纬度,经度
function getDistances(lat1, lng1, lat2, lng2) {
	let EARTH_RADIUS = 6378.137;// 地球半径
	let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
	let radLat2 = lat2 * Math.PI / 180.0;
	let a = radLat1 - radLat2;
	let 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 = s * EARTH_RADIUS; 
	s = Math.round(s * 10000) / 10000;// 输出为公里
	return { m: s * 1000, km: Number(s.toFixed(2)) }
}

//console.log(that.getDistance(第一点的纬度, 第一点的经度, 第二点的纬度, 第二点的经度))

//1
console.log(getDistances(39.94786644141805,116.3625827163144,39.837767797378675,116.42803348250504))
//2
console.log(getDistances(39.94786644141805,116.3625827163144,39.83065011412984,115.96920751324556))
//3
console.log(getDistances(40.00765912163315,116.10860936708505,39.989197820890126,116.30247949494124))
console 命令行工具 X clear

                    
>
console