目录

Coordinate Rectification

Introduction to Coordinate Systems

The Huawei map involves the following coordinate systems:

Coordinate Systems Used by the Huawei Map

Sites in the Chinese mainland, Hong Kong (China), and Macao (China) use the GCJ02 coordinate system, and sites in Taiwan (China) and outside China use the WGS84 coordinate system.

Use Cases

Map Kit uses the GCJ02 coordinate system for the Chinese mainland, Hong Kong (China), and Macao (China). Directly overlaying WGS84 coordinates on a map using the GCJ02 coordinate system will result in positional offsets due to difference in coordinate values. Therefore, to properly use Map Kit services in the Chinese mainland, Hong Kong (China), and Macao (China), WGS84 coordinates must be converted to GCJ02 coordinates first.

API Description

The following table describes APIs for coordinate rectification, which are mainly provided by the convertCoordinateSync and rectifyCoordinate methods in the map namespace. For more information about the APIs, please refer to API Reference.

API Description
mapCommon.CoordinateType Coordinate system type.
convertCoordinateSync(fromType: mapCommon.CoordinateType, toType: mapCommon.CoordinateType, location: mapCommon.LatLng): mapCommon.LatLng Converts WGS84 coordinates to GCJ02 coordinates.
rectifyCoordinate(context: common.Context, locations: Array<mapCommon.CoordinateLatLng>): Promise<Array<mapCommon.CoordinateLatLng>> Performs coordinate rectification.
mapCommon.LatLng Longitude-latitude object.

Development Procedure

Import required modules.

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

[h2]Coordinate Rectification

The rectifyCoordinate API checks whether rectification is required based on the input coordinates, input coordinate system, and current routing location. If yes, this API returns the rectified coordinate system and coordinates.

  • The rectifyCoordinate API is solely designed to address the display offset of original coordinates on a Huawei map.

  • The rectifyCoordinate API supports only conversion from WGS84 coordinates to GCJ02 coordinates.

let locations: Array<mapCommon.CoordinateLatLng> = [
  {
    // Enter coordinates of Hong Kong (China) and the WGS84 coordinate system. If the current map site uses the GCJ02 coordinate system, the GCJ02 coordinate system and the coordinates converted from WGS84 to GCJ02 are returned.
    coordinateType: mapCommon.CoordinateType.WGS84,
    location: {
      latitude: 22.280556,
      longitude: 114.984000
    }
  }
];
// The method containing await needs to use the async keyword.
let arr: Array<mapCommon.CoordinateLatLng> = await map.rectifyCoordinate(this.getUIContext().getHostContext(), locations);

[h2]Coordinate Conversion

Initialize the coordinates to be converted and call the convertCoordinateSync method to convert the coordinates.

let wgs84Position: mapCommon.LatLng = {
  latitude: 30,
  longitude: 118
};
// Convert the longitude-latitude coordinates.
let gcj02Position: mapCommon.LatLng =
  map.convertCoordinateSync(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, wgs84Position);