使用uni.getLocation获取经纬度不准确解决方法
微信开发工具上直接使用uni.getLocation获取经纬度不准确,有几百米的偏移,加入下列代码即可解决:
//加偏移
let x = longitude
let y = latitude
let x_pi = (3.14159265358979324 * 3000.0) / 180.0
let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi)
let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi)
let lngs = z * Math.cos(theta) + 0.0065
let lats = z * Math.sin(theta) + 0.006
console.log('加偏移经度:' + lngs + ',加偏移纬度:' + lats);
//加偏移结束
完整的代码如下:
/**
* 获取经纬度
*/
getLocation(){
var that =this;
uni.getLocation({
isHighAccuracy: true, // 开启地图精准定位
type: 'gcj02', // 坐标系类型
success: function (res) {
var latitude = res.latitude; // 维度
var longitude = res.longitude; // 经度
console.log('经度:' + longitude + ',