page.htmlhtml
<text bindtap="getAddress">{{addr}}</text>
page.jsjson
app.js小程序
//获取用户地理位置权限
getPermission: function (obj) {
wx.chooseLocation({
success: function (res) {
obj.setData({
addr: res.name //调用成功直接设置地址
})
},
fail: function () {
wx.getSetting({
success: function (res) {
var statu = res.authSetting;
if (!statu['scope.userLocation']) {
wx.showModal({
title: '是否受权当前位置',
content: '须要获取您的地理位置,请确认受权,不然地图功能将没法使用',
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.userLocation"] === true) {
wx.showToast({
title: '受权成功',
icon: 'success',
duration: 1000
})
//受权成功以后,再调用chooseLocation选择地方
wx.chooseLocation({
success: function (res) {
obj.setData({
addr: res.name
})
},
})
} else {
wx.showToast({
title: '受权失败',
icon: 'success',
duration: 1000
})
}
}
})
}
}
})
}
},
fail: function (res) {
wx.showToast({
title: '调用受权窗口失败',
icon: 'success',
duration: 1000
})
}
})
}
})
},
app.jsonapp
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展现"
}
},