我們先獲取定位,如果用戶拒絕或沒有開手機定位都會進入fail里的getSetFun函數(shù)轻纪。通過跳轉(zhuǎn)到首頁已達到用戶不開啟定位或授權(quán)不能進行下一步。代碼如下:
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: '是否授權(quán)當(dāng)前位置',
content: '請確認(rèn)授權(quán),否則無法正常使用',
success(res) {
if (res.confirm) {
wx.openSetting({
success() {
// 跳到首頁
}
})
} else if (res.cancel) {
// 跳到首頁
}
}
})
} else {
//用戶已授權(quán)涩嚣,但是獲取地理位置失敗崇众,提示用戶去系統(tǒng)設(shè)置中打開定位
wx.showModal({
title: '您手機定位功能沒有開啟',
content: '請在系統(tǒng)設(shè)置中打開定位服務(wù)',
success() {
// 跳到首頁
}
})
}
}
})
}