/**

 * 获取两个经纬度坐标正北方向夹角

 * @param {Array} o_latlngs 原点经纬度坐标 [经度, 纬度]

 * @param {Array} latlngs 经纬度坐标

 * @return {Number} 返回角度

 */

function getTwoPointAngle(o_latlngs, latlngs) {

    let A = new MyLatLng(o_latlngs[0], o_latlngs[1]);

    let B = new MyLatLng(latlngs[0], latlngs[1]);

    // console.log(getAngle(B, A));

    return getAngle(B, A);

}

/**

 * 求B点经纬度

 * @param A 已知点的经纬度,

 * @param distance   AB两地的距离  单位km

 * @param angle  AB连线与正北方向的夹角(0~360)

 * @return  B点的经纬度

 */

function getMyLatLng(A, distance, angle) {

    let dx = distance * 1000 * Math.sin(Math.toRadians(angle));

    let dy = distance * 1000 * Math.cos(Math.toRadians(angle));

    let bjd = ((dx / A.Ed + A.m_RadLo) * 180.0) / Math.PI;

    let bwd = ((dy / A.Ec + A.m_RadLa) * 180.0) / Math.PI;

    return new MyLatLng(bjd, bwd);

}

/**

 * 获取AB连线与正北方向的角度

 * @param A  A点的经纬度

 * @param B  B点的经纬度

 * @return  AB连线与正北方向的角度(0~360)

 */

function getAngle(A, B) {

    let dx = (B.m_RadLo - A.m_RadLo) * A.Ed;

    let dy = (B.m_RadLa - A.m_RadLa) * A.Ec;

    let angle = 0.0;

    angle = (Math.atan(Math.abs(dx / dy)) * 180.0) / Math.PI;

    let dLo = B.m_Longitude - A.m_Longitude;

    let dLa = B.m_Latitude - A.m_Latitude;

    if (dLo > 0 && dLa <= 0) {

        angle = 90.0 - angle + 90;

    } else if (dLo <= 0 && dLa < 0) {

        angle = angle + 180.0;

    } else if (dLo < 0 && dLa >= 0) {

        angle = 90.0 - angle + 270;

    }

    return angle;

}

class MyLatLng {

    constructor(lng, lat) {

        const Rc = 6378137; // 赤道半径

        const Rj = 6356725; // 极半径

        // 经度转为度分秒

        this.m_LoDeg = lng;

        this.m_LoMin = (lng - this.m_LoDeg) * 60;

        this.m_LoSec = (lng - this.m_LoDeg - this.m_LoMin / 60.0) * 3600;

        // 纬度转为度分秒

        this.m_LaDeg = lat;

        this.m_LaMin = (lat - this.m_LaDeg) * 60;

        this.m_LaSec = (lat - this.m_LaDeg - this.m_LaMin / 60.0) * 3600;

        this.m_Longitude = lng;

        this.m_Latitude = lat;

        this.m_RadLo = (lng * Math.PI) / 180.0; // 经度弧度

        this.m_RadLa = (lat * Math.PI) / 180.0; // 纬度弧度

        this.Ec = Rj + ((Rc - Rj) * (90.0 - this.m_Latitude)) / 90.0;

        this.Ed = this.Ec * Math.cos(this.m_RadLa);

    }

}

function getrst(sPoint, ePoint) {

    let rst = getTwoPointAngle(ePoint, sPoint);

    return rst;

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐