1.安卓:getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF'
2.iphone: getLocation:fail system permission denied'
3.部分安卓老机型(博主目前只在部分vivo,oppo的安卓9之前的版本中发现):getLocation:fail:system permission denied
ex:
// 地理位置授权
async getUserLocation(): Promise<void> {
const that = this;
wx.getSetting({
success: (res) => {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
// res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
// res.authSetting['scope.userLocation'] == true 表示 地理位置授权
// 拒绝授权后再次进入重新授权
if (res.authSetting['scope.userLocation'] !== undefined && res.authSetting['scope.userLocation'] !== true) {
// console.log('authSetting:status:拒绝授权后再次进入重新授权', res.authSetting['scope.userLocation'])
wx.showModal({
title: '未授权地理位置',
content: '您未授权地理位置,将导致小程序部分功能不可用\r\n您可在设置中打开地理位置开关',
confirmText: '去设置',
cancelText: '下次再说',
success(ress) {
if (ress.cancel) {
wx.showToast({
title: '拒绝授权',
icon: 'none',
});
// setTimeout(() => {
// wx.navigateBack();
// }, 1500);
} else if (ress.confirm) {
wx.openSetting({
success(dataAu) {
// console.log('dataAu:success', dataAu)
if (dataAu.authSetting['scope.userLocation'] === true) {
// 再次授权,调用wx.getLocation的API
that.getLocation(dataAu);
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
});
// setTimeout(() => {
// wx.navigateBack();
// }, 1500);
}
},
});
}
},
});
}
// 初始化进入,未授权
else if (res.authSetting['scope.userLocation'] === undefined) {
// console.log('authSetting:status:初始化进入,未授权', res.authSetting['scope.userLocation'])
// 调用wx.getLocation的API
that.getLocation(res);
}
// 已授权
else if (res.authSetting['scope.userLocation']) {
// console.log('authSetting:status:已授权', res.authSetting['scope.userLocation'])
// 调用wx.getLocation的API
that.getLocation(res);
}
},
});
}
// 微信获得经纬度
getLocation(userLocation: any): void {
const that = this;
wx.getLocation({
type: 'wgs84',
success(res) {
console.log('getLocation:success', res);
const { latitude } = res;
const { longitude } = res;
that.setData({
latitude,
longitude,
noLocation: false,
});
},
fail(res) {
console.log('getLocation:fail', res);
if (res.errMsg === 'getLocation:fail:auth denied') {
wx.showToast({
title: '拒绝授权',
icon: 'none',
});
// setTimeout(() => {
// wx.navigateBack();
// }, 1500);
return;
}
if (!userLocation || !userLocation.authSetting['scope.userLocation']) {
that.getUserLocation();
} else if (userLocation.authSetting['scope.userLocation']) {
if (
res.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' ||
res.errMsg === 'getLocation:fail system permission denied' ||
res.errMsg === 'getLocation:fail:system permission denied'
) {
that.setData({
noLocation: true,
});
dialog
.alert({
title: '请在系统设置中打开定位服务',
message: '',
confirmButtonText: '我知道了',
})
.then(() => {
console.log('点击了确定按钮');
});
}
return;
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
});
// setTimeout(() => {
// wx.navigateBack();
// }, 1500);
return;
}
},
});
}