我们先获取定位,如果用户拒绝或没有开手机定位都会进入fail里的getSetFun函数。通过跳转到首页已达到用户不开启定位或授权不能进行下一步。代码如下:
onLoad(){
let that = this;
wx.getLocation({
type: 'gcj02',
success(res) {
},
fail(error) {
that.getSetFun(); // 没有获取到位置,不停获取
}
})
}
// 没有获取到位置,不停获取
getSetFun() {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '是否授权当前位置',
content: '请确认授权,否则无法正常使用',
success(res) {
if (res.confirm) {
wx.openSetting({
success() {
// 跳到首页
}
})
} else if (res.cancel) {
// 跳到首页
}
}
})
} else {
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
wx.showModal({
title: '您手机定位功能没有开启',
content: '请在系统设置中打开定位服务',
success() {
// 跳到首页
}
})
}
}
})
}