function getDistance(lat1, lng1, lat2, lng2) {
var radLat1 = toRadians(lat1);
var radLat2 = toRadians(lat2);
var deltaLat = radLat1 - radLat2;
var deltaLng = toRadians(lng1) - toRadians(lng2);
var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2)
+ Math.cos(radLat1) *
Math.cos(radLat2) *
Math.pow(Math.sin(deltaLng / 2), 2)));
return dis * 6378137;
function toRadians(d) {
return d * Math.PI / 180;
}
}
var radLat1 = toRadians(lat1);
var radLat2 = toRadians(lat2);
var deltaLat = radLat1 - radLat2;
var deltaLng = toRadians(lng1) - toRadians(lng2);
var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2)
+ Math.cos(radLat1) *
Math.cos(radLat2) *
Math.pow(Math.sin(deltaLng / 2), 2)));
return dis * 6378137;
function toRadians(d) {
return d * Math.PI / 180;
}
}
console.log(getDistance(51.503353,-0.127556,51.51368,-0.099561))
得出结果:2254.71880313315 实际距离:2851.9 相差几百米还好
当前还没有任何评论