微信小程序-位置授权-2020-2-14
1、app.json
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口
}
}
2、.js
onShow: function () {
//初始获取定位权限
wx.authorize({
scope: 'scope.userLocation',
success: (res) => {
},
})
},
getLocation:function(e){
var that = this
wx.getSetting({
success(res){
//这里判断是否有地位权限
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '提示',
content: '请求获取位置权限',
success:function(res){
if(res.confirm==false){
return false;
}
wx.openSetting({
success(res) {
//如果再次拒绝则返回页面并提示
if (!res.authSetting['scope.userLocation']) {
wx.showToast({
title: '此功能需获取位置信息,请重新设置',
duration: 3000,
icon: 'none'
})
} else {
//允许授权,调用地图
that.chooseMap()
}
}
})
}
})
} else {
//如果有定位权限,调用地图
that.chooseMap()
}
}
})
},
chooseMap(){
var that = this
wx.chooseLocation({
success: function (res) {
that.setData({
address: res.address,
latitude: res.latitude,
longitude: res.longitude
})
},
fail: function (res) {
console.log(res)
}
})
},
3、.wxml
<button class="pre" size="mini" type="default" bindtap='getLocation'>选择位置</button>