/**
* 已知两地经纬度求得直线距离
* @param {Number} lat1 纬度1
* @param {Number} lng1 经度1
* @param {Number} lat2 纬度2
* @param {Number} lng2 经度2
* @returns {Number} 距离(米)
* @example
* distanceBetweenTwoPlaces(39.915, 116.404, 39.975, 116.494)
* // => 11149.578
*/
export function distanceBetweenTwoPlaces(lat1, lng1, lat2, lng2) {
const radLat1 = (lat1 * Math.PI) / 180.0;
const radLat2 = (lat2 * Math.PI) / 180.0;
const a = radLat1 - radLat2;
const 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 * 6378.137; // EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s * 1000;
}
【前端纯js】已知两地经纬度求得直线距离
最新推荐文章于 2024-04-10 22:45:34 发布