js 计算两个坐标点的距离
2020-03-17 本文已影响0人
没见过的颜色
let PI = 3.14159265358979323;//圆周率
let R = 6371229;//地球半径
function getDistance(lon1,lat1,lon2,lat2){
let x,y,distance;
let lonres = lon1 > lon2?lon1-lon2:lon2-lon1;
let latres = lat1 > lat2?lat1-lat2:lat2-lat1;
x = (lonres) * PI * R * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
y = (lat2 - lat1) * PI * R / 180;
return distance = Math.hypot(x, y);
}