接口返回的经纬度是在合家欢公寓,而小程序的openLocation 地图实际却定位到了玖耀里,导致了将近1km的偏差
因为我是从第三方拿的经纬度位置,随后我又开始全网搜索找答案,有说 加 type:‘gcj02’ 的。经测试,这个仅对获取当前位置 getLocation生效,打开位置openLocation并不生效。后来猜想是百度经纬度与腾讯的有差异。随后搜了段代码,把传入经纬度转一下就OK了!
let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
let x = longitude - 0.0065;
let y = latitude - 0.006;
let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
let wz = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
let lon = z * Math.cos(wz);
let lat = z * Math.sin(wz);
longitude=lon;
latitude=lat;
实例代码
methods:{
getLocation(latitude,longitude,name){
let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
let x = longitude - 0.0065;
let y = latitude - 0.006;
let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
let wz = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
let lon = z * Math.cos(wz);
let lat = z * Math.sin(wz);
// latitude = lat
// longitude = lon
console.log(lon,'longitude');
wx.openLocation({
latitude: lat,
longitude: lon,
scale:15,
name:name,
success: function () {
console.log('success');
},
fail(e) {
console.log(e,'fail');
}
});
},
}