因为华为地图只提供了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
};
}