Files
ttstd_family_care_miniapp/utils/coord.ts
T

99 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 坐标转换工具(边界适配,只在落图前调用一次)。
*
* 全链路约定:后端内部统一 GCJ-02,DeviceLocationVO 出参带 `coordType`。
* 微信原生 <map> 与 wx.openLocation 只认 GCJ-02,所以正常情况是直通。
* 这里保留 BD-09 / WGS-84 分支,用于兼容尚未升级的老后端数据。
*/
const PI = Math.PI
const A = 6378245.0
const EE = 0.00669342162296594323
export interface LatLng {
latitude: number
longitude: number
}
/** 国外坐标不做偏移(国内坐标系只在国内生效)。 */
function outOfChina(lng: number, lat: number): boolean {
return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271
}
function transformLat(x: number, y: number): number {
let ret =
-100.0 +
2.0 * x +
3.0 * y +
0.2 * y * y +
0.1 * x * y +
0.2 * Math.sqrt(Math.abs(x))
ret += ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) / 3.0
ret += ((20.0 * Math.sin(y * PI) + 40.0 * Math.sin((y / 3.0) * PI)) * 2.0) / 3.0
ret += ((160.0 * Math.sin((y / 12.0) * PI) + 320 * Math.sin((y * PI) / 30.0)) * 2.0) / 3.0
return ret
}
function transformLng(x: number, y: number): number {
let ret =
300.0 +
x +
2.0 * y +
0.1 * x * x +
0.1 * x * y +
0.1 * Math.sqrt(Math.abs(x))
ret += ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) / 3.0
ret += ((20.0 * Math.sin(x * PI) + 40.0 * Math.sin((x / 3.0) * PI)) * 2.0) / 3.0
ret += ((150.0 * Math.sin((x / 12.0) * PI) + 300.0 * Math.sin((x / 30.0) * PI)) * 2.0) / 3.0
return ret
}
/** BD-09(百度经纬度)→ GCJ-02:先去掉百度偏移,再做边界判定。 */
export function bd09ToGcj02(lng: number, lat: number): LatLng {
const x = lng - 0.0065
const y = lat - 0.006
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI)
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI)
const gcjLng = z * Math.cos(theta)
const gcjLat = z * Math.sin(theta)
if (outOfChina(gcjLng, gcjLat)) {
return { latitude: lat, longitude: lng }
}
return { latitude: gcjLat, longitude: gcjLng }
}
/**
* 按后端 coordType 统一转 GCJ-02。
* 老后端不返回 coordType 且主坐标为 BD-09,缺省按 BD-09 兼容。
*/
export function toGcj02(lng: number, lat: number, coordType?: string): LatLng {
const type = (coordType || 'BD09LL').toUpperCase()
if (type === 'GCJ02' || type === 'GCJ02LL' || type === 'COMMON') {
return { latitude: lat, longitude: lng }
}
if (type === 'WGS84' || type === 'WGS84LL') {
return wgs84ToGcj02(lng, lat)
}
return bd09ToGcj02(lng, lat)
}
/**
* WGS84 → GCJ-02(保留给海外数据或将来 WGS84 来源)。
*/
export function wgs84ToGcj02(lng: number, lat: number): LatLng {
if (outOfChina(lng, lat)) {
return { latitude: lat, longitude: lng }
}
let dLat = transformLat(lng - 105.0, lat - 35.0)
let dLng = transformLng(lng - 105.0, lat - 35.0)
const radLat = (lat / 180.0) * PI
let magic = Math.sin(radLat)
magic = 1 - EE * magic * magic
const sqrtMagic = Math.sqrt(magic)
dLat = (dLat * 180.0) / (((A * (1 - EE)) / (magic * sqrtMagic)) * PI)
dLng = (dLng * 180.0) / ((A / sqrtMagic) * Math.cos(radLat) * PI)
return { latitude: lat + dLat, longitude: lng + dLng }
}