当跳转到让用户授权时,有的用户有位置信息,有的没有,所以无法使用地图
map: function () {
var that = this
wx.getSetting({
success(res) {
console.log('res是否开启授权', res)
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
success() {
// console.log('前用户发起授权请求')
wx.chooseLocation({
success: res => {
console.log('打开地图选择位置确定', res)
},
fail: res => {
console.log('打开地图选择位置取消', res)
}
})
},
fail() {
// 用户点击不允许引导重新获取授权
that.fetchAgainLocation()
}
})
} else {
// 已经授权了就会直接进入地图
wx.chooseLocation({
success: res => {
console.log('打开地图选择位置确定', res)
},
fail: res => {
console.log('打开地图选择位置取消', res)
}
})
}
}
})
uni.chooseLocation({
success: function (res) {
if (res.name == '') {
} else {
that.addr = res.address
that.userDetailAddress = res.name
that.latitude = res.latitude
that.longitude = res.longitude
console.log('纬度:' + res.latitude);
console.log('经度:' + res.longitude);
}
},
fail: function () {
},
complete: function () {
}
})
},
fetchAgainLocation() {
let that = this
wx.getSetting({
success: (res) => {
var statu = res.authSetting;
if (!statu['scope.userLocation']) {
wx.showModal({
title: '是否授权当前位置',
content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
success: (tip) => {
if (tip.confirm) {
wx.openSetting({
success: (data) => {
if (data.authSetting["scope.userLocation"] === true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
wx.chooseLocation({
success: res => {
console.log('打开地图选择确定', res)
}
})
} else {
wx.showToast({
title: '授权失败',
icon: 'success',
duration: 1000
})
}
},
fail: () => {},
complete: () => {}
});
}
}
})
}
},
fail: ()=>{},
complete: ()=>{}
})
},
确认下用户是否已经授权