//uniapp定位
uni.getLocation({
type: 'wgs84',
isHighAccuracy: true,
success: function(res) {
// console.log("_location:", res)
let centerXY = this._coordinateShift(res.longitude, res.latitude)//解决坐标偏移方法
},
fail: function(error) {
console.error('获取位置失败:', error);
// 检查权限
if (err.errMsg.includes('deny')) {
uni.showModal({
title: '提示',
content: '请在设置中打开定位服务和允许使用定位',
success: function(modalRes) {
if (modalRes.confirm) {
// 引导用户去设置中开启权限
uni.openSetting();
}
}
});
}
}
});
//uni定位坐标偏移
_coordinateShift(longitude, latitude) {
const pi = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
let lon = longitude; // 经度
let lat = latitude; // 维度
let dLat = transformLat(lon - 105.0, lat - 35.0);
let dLon = transformLon(lon - 105.0, lat - 35.0);
let radLat = lat / 180.0 * pi;
let magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
let sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
let mgLat = lat + dLat;
let mgLon = lon - dLon;
function transformLat(x, y) {
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 transformLon(x, y) {
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;
}
return {x:mgLon, y:mgLat}
},