【问题描述】

直接使用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 + ',纬度:' + latitude);
			// that.form.LATITUDE = latitude;
			// that.form.LONGITUDE = longitude;
			//加偏移
			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);
			//加偏移结束
			console.log('当前位置的经度:' + lngs);
			console.log('当前位置的纬度:' + lats);
			console.log('当前速度:' + res.speed + '米/秒');
	       
			
				
	    },
	    fail: function (res) {
	        console.log('获取定位失败:' + res.errMsg);
	    }
	});
},


 

【参考文档】

uniapp 定位:

uni.getLocation(OBJECT) | uni-app官网

更多推荐