HarmonyOS Next 華為地圖與百度地圖坐標(biāo)系轉(zhuǎn)換

因?yàn)槿A為地圖只提供了WGS84轉(zhuǎn)GCJ02,詳見convertCoordinate
項(xiàng)目中由于使用的是百度地圖坐標(biāo),因此需要轉(zhuǎn)換。下為非官方轉(zhuǎn)換,準(zhǔn)確度待檢驗(yàn)

import { mapCommon } from '@kit.MapKit';

export class CoordTransform {
  // 定義一些常量
  static x_PI: number = 3.14159265358979324 * 3000.0 / 180.0;
  static PI: number = 3.1415926535897932384626;
  static a: number = 6378245.0;
  static ee: number = 0.00669342162296594323;

  /**
   * 百度坐標(biāo)系 (BD-09) 與 火星坐標(biāo)系 (GCJ-02) 的轉(zhuǎn)換
   * 即 百度 轉(zhuǎn) 華為、高德
   * @param bd_lng
   * @param bd_lat
   * @returns {*[]}
   */
  static async bd09togcj02(bd_lng: number, bd_lat: number): Promise<mapCommon.LatLng> {
    let x = bd_lng - 0.0065;
    let y = bd_lat - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * CoordTransform.x_PI);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * CoordTransform.x_PI);
    let gg_lng = z * Math.cos(theta);
    let gg_lat = z * Math.sin(theta);
    return { "longitude": gg_lng, "latitude": gg_lat } as mapCommon.LatLng
  };

  /**
   * 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換
   * 即 華為、高德 轉(zhuǎn) 百度
   * @param lng
   * @param lat
   * @returns {*[]}
   */
  static async gcj02tobd09(lng: number, lat: number): Promise<mapCommon.LatLng> {
    let z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * CoordTransform.x_PI);
    let theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * CoordTransform.x_PI);
    let bd_lng = z * Math.cos(theta) + 0.0065;
    let bd_lat = z * Math.sin(theta) + 0.006;
    return { "longitude": bd_lng, "latitude": bd_lat } as mapCommon.LatLng
  };
}

參考http://www.itdecent.cn/p/22a1f8181bf2

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容