原生定位用高德,小程序用腾讯,某个固定地址用的百度,三方定位匹配,在同一个地方计算出来的距离总是相差十万八千里?
其实就是各个地图用的坐标系不同,导致在同一个地方的坐标不同。匹配出来导致的现象就是定位“飘了”。
最离谱的还是,我们在超高楼层,定位就很飘,还是去一楼排查的问题,人生体验+1了。
下面是把百度定位转换成和其他相同的统一坐标系的方法:
/**
* 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
* 百度地图使用的是BD-09坐标系
* @param bd_lon 百度经度
* @param bd_lat 百度纬度
*/
bd09ToGcj02(bd_lon, bd_lat) {
// 百度坐标系转火星坐标系 (BD-09 -> GCJ-02)
const x_pi = (Math.PI * 3000.0) / 180.0
const x = bd_lon - 0.0065
const y = bd_lat - 0.006
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
const gcj_lon = z * Math.cos(theta)
const gcj_lat = z * Math.sin(theta)
return {
longitude: gcj_lon,
latitude: gcj_lat
}
}