微信小程序地理位置授權
1、小程序中獲取當前的地理位置,需要用戶授權scope.userLocation
首先在app.json中配置
"permission": {
"scope.userLocation": {
"desc": "你的位置信息將用於計算與門店的距離"
}
}
2、判斷首次打開時用戶是否獲得了地理位置授權
//獲取定位
//判斷是否獲得了用戶地理位置授權
getSetting: function() {
let that = this;
wx.getSetting({
success: (res) => {
// 查看位置權限的狀態 如果是首次授權(undefined)或者之前拒絕授權(false)
//!res.authSetting['scope.userLocation']
if (res.authSetting['scope.userLocation'] == false) {
//之前拒絕授權(false)
that.openConfirm()
} else {
//如果是首次授權則彈出授權窗口進行授權,如果之前進行了授權,則獲取地理位置信息
that.getLocation()
}
}
})
},
openConfirm: function() {
let that = this;
wx.showModal({
content: '檢測到您沒打開定位權限,是否去設置打開?',
confirmText: "確認",
cancelText: "取消",
success: function(res) {
console.log(res);
//點擊“確認”時打開設置頁面
if (res.confirm) {
console.log('用戶點擊確認')
wx.openSetting({
success: (res) => {
that.getLocation()
}
})
} else {
console.log('用戶點擊取消')
}
}
});
},
getLocation: function() {
let that = this;
wx.getLocation({
type: 'gcj02',
altitude: true,
success: function(res) {},
fail: function(res) {
console.log("---未授權---");
wx.openSetting({})
},
complete: function(res) {},
})
},