When a user first accesses a page, the application calls wx.getLocation to request locatoin permissions. If the user denies the request, subesquent visits can check the current authorization status using wx.getSetting.
getLocation: function () {
var that = this;
wx.getLocation({
type: 'wgs84',
success: function (res) {
wx.setStorageSync('latitude', res.latitude);
wx.setStorageSync('longitude', res.longitude);
}
})
}
The process involves three key steps:
- Initial permission denial
- A prompt appears requestnig location access
- Successful authorization confirmation
again_getLocation: function(){
let that = this;
wx.getSetting({
success: (res) => {
console.log(res)
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
wx.showModal({
title: 'Location Access Required',
content: 'Access to your location is necessary to provide relevant data.',
success: function (res) {
if (res.cancel) {
that.setData({
isshowCIty: false
})
wx.showToast({
title: 'Authorization Denied',
icon: 'success',
duration: 1000
})
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: 'Authorization Granted',
icon: 'success',
duration: 1000
})
that.getLocation(that);
} else {
wx.showToast({
title: 'Authorization Failed',
icon: 'success',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
that.getLocation(that);
} else {
that.getLocation(that);
}
}
})
}