js计算两个经纬度之间的直线距离

/** 两点的直线距离 */
class Distance {
    /**
     * @class Distance
     * @method val
     * @param {number} start_lat - 起点的纬度
     * @param {number} start_lon - 起点的经度
     * @param {number} end_lat - 终点的纬度
     * @param {number} end_lon - 终点的经度
     * @return {number} 距离千米/公里
     * @description 根据两点的经纬度计算两点之间直线距离,实际路程建议直线距离乘以1.4
     * @example
     * import { distance } from '../../../utils/distance';
     * distance.val(40.1835390,115.823092,40.4411433,119.882540)
     */
    val(start_lat:number, start_lon:number, end_lat:number, end_lon:number):number {
        let radLat1 = (start_lat * Math.PI) / 180.0;
        let radLat2 = (end_lat * Math.PI) / 180.0;
        let a = radLat1 - radLat2;
        let b = (start_lon * Math.PI) / 180.0 - (end_lon * 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; // 地球半径;
        s = Math.round(s * 10000) / 10000;

        return s;
    }
}
export const distance = new Distance();

 

posted @ 2022-01-10 23:04  赵永强  阅读(583)  评论(0编辑  收藏  举报