gis开发中经常遇到计算两点坐标距离的方法,现总结如下:
const EARTH_RADIUS = 6378137; // 地球半径,单位:米。
function calcDistance(lon1: number, lat1: number, lon2: number, lat2: number): number { const radLat1 = lat1 * Math.PI / 180; const radLat2 = lat2 * Math.PI / 180; const a = radLat1 - radLat2; const b = (lon1 * Math.PI / 180) - (lon2 * Math.PI / 180); 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; return s; }
返回值s单位为 米 !!!!!!!!!!!