HarmonyOS鸿蒙Next中华为地图与百度地图坐标系转换

HarmonyOS鸿蒙Next中华为地图与百度地图坐标系转换 因为华为地图只提供了WGS84转GCJ02,详见convertCoordinate
项目中由于使用的是百度地图坐标,因此需要转换。下为非官方转换,准确度待检验

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;

  /**
   * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02) 的转换
   * 即 百度 转 华为、高德
   * @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
  };

  /**
   * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
   * 即 华为、高德 转 百度
   * @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
  };
}

参考https://www.jianshu.com/p/22a1f8181bf2


更多关于HarmonyOS鸿蒙Next中华为地图与百度地图坐标系转换的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中华为地图与百度地图坐标系转换的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中实现华为地图与百度地图的坐标系转换,通常需要进行经纬度坐标的转换。华为地图使用的是WGS84坐标系,而百度地图使用的是BD09坐标系。可以通过以下步骤实现转换:

  1. WGS84转GCJ02:首先将WGS84坐标转换为GCJ02坐标(中国国测局加密坐标)。
  2. GCJ02转BD09:再将GCJ02坐标转换为BD09坐标(百度地图使用的坐标系)。

可以使用现成的开源库或算法实现这一转换,如coordtransform库。具体代码示例如下:

// WGS84转GCJ02
function wgs84ToGcj02(lng, lat) {
    // 转换算法实现
}

// GCJ02转BD09
function gcj02ToBd09(lng, lat) {
    // 转换算法实现
}

// 使用示例
let wgs84Lng = 116.404;
let wgs84Lat = 39.915;
let gcj02 = wgs84ToGcj02(wgs84Lng, wgs84Lat);
let bd09 = gcj02ToBd09(gcj02.lng, gcj02.lat);

通过上述步骤,即可实现华为地图与百度地图坐标系的转换。

回到顶部