开发文档 : https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html
在 app.json
中添加 permission
字段, 否则在开发者工具上可以调起位置选择, 在手机上无法调起
"permission":{
"scope.userLocation":{
"desc":"请点击允许"
}
}
fail 回调常见返回值
// 用户未授权
{errMsg:"chooseLocation:fail auth deny"}
// 用户取消选择位置
{errMsg: "chooseLocation:fail cancel"}
未授权获取地理位置信息的判断
方案一: 根据 wx.getSetting()
判断
// 打开地图选择位置
wx.chooseLocation({
success: function (res) {
console.log(res)
},
fail: function (res) {
// 用户未授权
wx.getSetting({
success (res) {
if(!res.authSetting["scope.userLocation"]){
wx.showToast({
title: '请先授权 获取位置信息',
mask: true,
icon: 'none'
})
}
}
})
}
})
方案二: 根据返回值判断
// 打开地图选择位置
wx.chooseLocation({
success: function (res) {
console.log(res)
},
fail: function (res) {
// 用户未授权
if (res.errMsg == "chooseLocation:fail auth deny") {
wx.showToast({
title: '请先授权 获取位置信息',
mask: true,
icon: 'none'
})
}
}
})
获取成功时的返回值
{
errMsg: "chooseLocation:ok",
name: "郑州市金水区人民政府",
address: "河南省郑州市金水区东风路16号",
latitude: 34.79977,
longitude: 113.66072
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有