鸿蒙Next开发中如何将wgs84坐标转换为bd02坐标

在鸿蒙Next开发中,如何将WGS84坐标转换为BD09坐标?目前项目需要调用百度地图SDK,但设备获取的GPS坐标是WGS84格式,直接显示会有偏移。求教具体的转换算法或HarmonyOS提供的API实现方法,最好有代码示例说明转换过程。

2 回复

在鸿蒙Next中,将WGS84坐标转换为BD09坐标,可以借助第三方库如proj4js或手动实现转换公式。简单来说,先通过WGS84转GCJ02,再转BD09。代码示例(伪代码):

// WGS84转GCJ02
Coordinate gcj02 = wgs84ToGcj02(wgs84);
// GCJ02转BD09
Coordinate bd09 = gcj02ToBd09(gcj02);

记得处理坐标偏移的“火星”魔法!😄 具体实现可参考开源库或官方文档。

更多关于鸿蒙Next开发中如何将wgs84坐标转换为bd02坐标的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在鸿蒙Next开发中,要将WGS84坐标(国际通用GPS坐标系)转换为BD02坐标(百度地图坐标系),可以通过以下步骤实现:

1. 坐标转换原理

WGS84到BD02需要经过两步转换:

  • WGS84 → GCJ02(中国国测局坐标系,火星坐标)
  • GCJ02 → BD02(百度坐标系)

2. 核心代码示例

以下是基于HarmonyOS ArkTS的坐标转换工具类:

// 坐标转换工具类 CoordinateConverter.ts
export class CoordinateConverter {
  private static readonly PI: number = 3.1415926535897932384626;
  private static readonly A: number = 6378245.0;  // WGS84椭球长半轴
  private static readonly EE: number = 0.00669342162296594323; // WGS84椭球偏心率平方

  // WGS84转GCJ02
  public static wgs84ToGcj02(lng: number, lat: number): number[] {
    if (this.isOutOfChina(lng, lat)) {
      return [lng, lat];
    }
    let dLat = this.transformLat(lng - 105.0, lat - 35.0);
    let dLng = this.transformLng(lng - 105.0, lat - 35.0);
    const radLat = (lat / 180.0) * this.PI;
    let magic = Math.sin(radLat);
    magic = 1 - this.EE * magic * magic;
    const sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((this.A * (1 - this.EE)) / (magic * sqrtMagic) * this.PI);
    dLng = (dLng * 180.0) / (this.A / sqrtMagic * Math.cos(radLat) * this.PI);
    const mgLat = lat + dLat;
    const mgLng = lng + dLng;
    return [mgLng, mgLat];
  }

  // GCJ02转BD02
  public static gcj02ToBd02(lng: number, lat: number): number[] {
    const z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * this.PI);
    const theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * this.PI);
    const bdLng = z * Math.cos(theta) + 0.0065;
    const bdLat = z * Math.sin(theta) + 0.006;
    return [bdLng, bdLat];
  }

  // 完整转换:WGS84 → BD02
  public static wgs84ToBd02(lng: number, lat: number): number[] {
    const gcj02 = this.wgs84ToGcj02(lng, lat);
    return this.gcj02ToBd02(gcj02[0], gcj02[1]);
  }

  private static transformLat(lng: number, lat: number): number {
    let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * this.PI) + 40.0 * Math.sin(lat / 3.0 * this.PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lat / 12.0 * this.PI) + 320 * Math.sin(lat * this.PI / 30.0)) * 2.0 / 3.0;
    return ret;
  }

  private static transformLng(lng: number, lat: number): number {
    let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lng * this.PI) + 40.0 * Math.sin(lng / 3.0 * this.PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lng / 12.0 * this.PI) + 300.0 * Math.sin(lng / 30.0 * this.PI)) * 2.0 / 3.0;
    return ret;
  }

  private static isOutOfChina(lng: number, lat: number): boolean {
    return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271;
  }
}

3. 使用示例

// 在业务代码中使用
const wgs84Lng = 116.397428;  // 北京天安门WGS84经度
const wgs84Lat = 39.90923;    // 北京天安门WGS84纬度

const bd02Coord = CoordinateConverter.wgs84ToBd02(wgs84Lng, wgs84Lat);
console.log(`BD02坐标: [${bd02Coord[0]}, ${bd02Coord[1]}]`);

注意事项:

  1. 转换精度存在米级误差,适合常规地图显示
  2. 境外坐标直接返回原坐标(不进行转换)
  3. 建议在实际使用前进行坐标验证

此实现基于公开的坐标转换算法,可直接集成到鸿蒙Next应用中。

回到顶部