因?yàn)槿A為地圖只提供了WGS84轉(zhuǎn)GCJ02,詳見convertCoordinate
項(xiàng)目中由于使用的是百度地圖坐標(biāo),因此需要轉(zhuǎn)換。下為非官方轉(zhuǎn)換,準(zhǔn)確度待檢驗(yàn)
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;
/**
* 百度坐標(biāo)系 (BD-09) 與 火星坐標(biāo)系 (GCJ-02) 的轉(zhuǎn)換
* 即 百度 轉(zhuǎn) 華為、高德
* @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
};
/**
* 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換
* 即 華為、高德 轉(zhuǎn) 百度
* @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
};
}