如何解决HarmonyOS鸿蒙Next中地理编码后坐标在地图定位不准确的问题

如何解决HarmonyOS鸿蒙Next中地理编码后坐标在地图定位不准确的问题

【问题现象】

使用Location Kit的地理编码转换API出来的坐标直接在Map kit上展示,定位不准确。

示例代码如下:

// request 地址为:江苏省南京市雨花台区华为路华为云楼.onClick(() => {
  let geocodeRequest: geoLocationManager.GeoCodeRequest = { "description": location, "maxItems": 1 };
  try {
    geoLocationManager.getAddressesFromLocationName(geocodeRequest, (err, data) => {
      if (err) {
        console.log('getAddressesFromLocationName err: ' + JSON.stringify(err));
      } else {
        console.log('getAddressesFromLocationName data: ' + JSON.stringify(data));
      }
    });
  } catch (err) {
    console.error("errCode:" + JSON.stringify(err));
  }
})

坐标结果:

getAddressesFromLocationName data: [{"latitude":31.986409490839385,"longitude":118.76021387384216,"locale":"zh","placeName":"江苏省南京市雨花台区华为路华为云楼"}]

地图展示:

点击放大

如图所示,直接展示的坐标不准确。

【背景知识】

(1)使用坐标描述一个位置,非常准确,但是并不直观,面向用户表达并不友好。Location Kit向开发者提供了以下两种转化能力

  • 地理编码转化:将地理描述转化为具体坐标。
  • 逆地理编码转化能力:将坐标转化为地理描述。

本次问题现象即正地理编码转化后的坐标地址不准确。

(2)Location Kit返回的位置都是基于WGS84坐标系的,而国内地图应该使用GCJ02坐标系。

点击放大

【解决方案】

当展示的坐标不准确,优先检查坐标系差异,不一致时使用坐标转换接口将WGS84坐标转换为GCJ02坐标系再访问,坐标转换介绍请参考文档

WGS84转GCJ02 工具类示例代码如下:

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

export class MapUtil {
  public static convertToGcj02( latitude: number, longitude: number) {
    let wgs84Position: mapCommon.LatLng = {
      latitude: latitude,
      longitude: longitude
    };

    // 转换经纬度坐标
    let gcj02Position: mapCommon.LatLng =
      map.convertCoordinateSync(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, wgs84Position);
    return gcj02Position
  }
}

地图初始化示例代码如下:

aboutToAppear(): void {
    this.latitude = 31.986409490839385
    this.longitude = 118.76021387384216
    let gcj02Position = MapUtil.convertToGcj02(this.latitude, this.longitude)

    // 地图初始化参数
    this.mapOptions = {
      position: {
        target: {
          latitude: gcj02Position.latitude,
          longitude: gcj02Position.longitude
        },
        zoom: 16,
      },
      myLocationControlsEnabled: true
    };

    this.callback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
        // 创建Marker
        let markerOptions: mapCommon.MarkerOptions = {
          position: {
              latitude: gcj02Position.latitude,
              longitude: gcj02Position.longitude
          }
        };
        this.mapController.addMarker(markerOptions);
      }
    };
  }

效果示例图如下:

点击放大


更多关于如何解决HarmonyOS鸿蒙Next中地理编码后坐标在地图定位不准确的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中,地理编码后坐标定位不准确可能是由于坐标系不匹配或数据源精度问题

确保使用的地图服务与地理编码服务坐标系一致,如WGS84。检查地理编码API返回的坐标精度,必要时使用更高精度的数据源。此外,确认设备定位权限已开启,并确保网络连接稳定。

更多关于如何解决HarmonyOS鸿蒙Next中地理编码后坐标在地图定位不准确的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


问题分析

地理编码后坐标定位不准确的主要原因是坐标系不匹配:

  • Location Kit返回的是WGS84坐标系坐标
  • 国内地图需要使用GCJ02坐标系

解决方案

使用MapKit的坐标转换接口将WGS84转为GCJ02:

1. 创建转换工具类

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

export class MapUtil {
  public static convertToGcj02(latitude: number, longitude: number) {
    let wgs84Position: mapCommon.LatLng = {
      latitude: latitude,
      longitude: longitude
    };
    
    return map.convertCoordinateSync(
      mapCommon.CoordinateType.WGS84,
      mapCommon.CoordinateType.GCJ02,
      wgs84Position
    );
  }
}

2. 地图初始化时转换坐标

let gcj02Position = MapUtil.convertToGcj02(latitude, longitude);

this.mapOptions = {
  position: {
    target: {
      latitude: gcj02Position.latitude,
      longitude: gcj02Position.longitude
    },
    zoom: 16
  }
};

注意事项

  1. 确保导入正确的MapKit模块
  2. 转换后的坐标才能用于国内地图展示
  3. 添加Marker时也要使用转换后的坐标
回到顶部