小程序地图插件获取授权的时候,用户点了取消,那么就会被默认无法授权,需要用wx.openSetting()方法来打开当前小程序所需要的授权,用户手动开启。(注意,这个方法只有用户自己点击才会生效,所以需要加入过渡页诱导)
解决方法:就是在地图插件页面销毁时,做一步判定,map.js
//页面销毁时判断位置信息是否授权
onUnload: function () {
wx.getSetting({
success: function (res) {
if (!res.authSetting['scope.userLocation']) {
wx.openSetting({
success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
},
fail(err) {
//未授权则跳转到中间页
wx.navigateTo({
url: './hint',
})
}
})
}
}
})
},
中间页随便啦,就是告诉别人你为毛需要这个授权,诱导别人开启

点击后

hint.js的按钮执行代码如下
go(){
wx.getSetting({
success: function (res) {
if (!res.authSetting['scope.userLocation']) {
wx.openSetting({
success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
//简单来说就是用户授权了,就让他再进入地图插件页面
wx.navigateTo({
url: './BMap',
})
},
fail(err) {
console.log(err)
}
})
}
}
})
},
然后就能正常使用了
2084

被折叠的 条评论
为什么被折叠?



