getLocation errCode: 2 获取地理位置失败?
在获取定位信息的时候,api报错,麻烦帮看一下是什么问题??
const authorizeLocation = () => {
if (location?.latitude && location?.longitude) return; // 当前已经设置了
wx.getSetting().then(async (res) => {
if (res.errMsg === "getSetting:ok") {
// 如果没有授权获取地理位置
let hasAuthrized = 'scope.userLocation' in res.authSetting; // 说明已经授过权了 (有可能中途取消);
let authrized = !!res.authSetting['scope.userLocation'];
// 从未授权过 -> 去授权
if (!hasAuthrized) {
try {
const authRes = await Taro.authorize({
scope: "scope.userLocation",
});
authrized = authRes.errMsg === "authorize:ok";
} catch (err) {
authrized = false;
}
}
// 拒绝授权
if (!authrized) {
return Taro.showModal({
title: 'xxxx',
content: 'xxxx',
cancelText: '取消',
confirmText: '前往授权',
success: (r) => {
if (r.confirm) {
Taro.openSetting({
success: (setting) => {
// 判断获取地理位置授权是否变化
// 变化则重新走授权的流程
if (setting.authSetting['scope.userLocation']) {
authorizeLocation();
}
}
});
}
}
})
}
// 授权通过 去获取地理位置
try {
const l = await Taro.getLocation({ type: 'gcj02', isHighAccuracy: true });
setLocation(l);
} catch (err) {
console.log(err);
}
const startRes: any = await Taro.startLocationUpdate()
if (startRes.errMsg === 'startLocationUpdate:ok') {
Taro.offLocationChange(setLocation as any);
Taro.onLocationChange(setLocation);
}
} else {
console.error(res);
}
});
}
设备信息等: [图片] 错误信息 getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF [图片]