async onLocation(){
// authSetting 获取小程序已经申请的权限,并返回授权结果
const { authSetting } = await wx.getSetting()
// scope.userLocation 是否授权小程序获取位置信息
// 是否拒绝了授权
if(authSetting['scope.userLocation'] === false){
// 拒绝授权之后,再次点击授权
wx.showModal({
title: '授权提示',
content: '是否要获取地理位置信息',
complete: (res) => {
if (res.cancel) {
// 点击取消,拒绝授权,可以弹框提示
}
if (res.confirm) {
// 点击确定,同意授权,需要打开微信小程序授权页面
const { authSetting } = await wx.openSetting()
if(!authSetting['scope.userLocation']){
// 授权失败逻辑
return
}
// 同意授权,更新授权信息
try {
const res = await wx.getLocation()
} catch (error) {
// 拒绝授权,此处可以弹框显示拒绝授权信息
}
}
}
})
} else{
try {
const res = await wx.getLocation()
} catch (error) {
// 拒绝授权,此处可以弹框显示拒绝授权信息
}
}
}