You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
936 B
37 lines
936 B
/**
|
|
* @desc 根据两点间的经纬度计算距离
|
|
* @param float $lat 纬度值
|
|
* @param float $lng 经度值
|
|
*/
|
|
const getDistance = (lat1, lon1, lat2, lon2) => {
|
|
console.log(lat1)
|
|
console.log(lon1)
|
|
console.log(lat2)
|
|
console.log(lon2)
|
|
let ew1, ns1, ew2, ns2;
|
|
let distance;
|
|
// 角度转换为弧度
|
|
ew1 = lon1 * 0.01745329252;
|
|
ns1 = lat1 * 0.01745329252;
|
|
ew2 = lon2 * 0.01745329252;
|
|
ns2 = lat2 * 0.01745329252;
|
|
distance = distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) *
|
|
Math.cos(ew1 - ew2);
|
|
// 调整到[-1..1]范围内,避免溢出
|
|
if (distance > 1.0)
|
|
distance = 1.0;
|
|
else if (distance < -1.0)
|
|
distance = -1.0;
|
|
// 求大圆劣弧长度
|
|
distance = 6370693.5 * Math.acos(distance);
|
|
let isBig = false; // 是否为大于等于1000m
|
|
if (distance >= 1000) {
|
|
distance /= 1000;
|
|
isBig = true;
|
|
}
|
|
|
|
return distance.toFixed(2) + (isBig ? "km" : "m");
|
|
|
|
}
|
|
|
|
export default getDistance
|
|
|