使用 wx.getLocation() 获取用户位置并转化成真实描述文字
解决问题:
设置用户授权小程序中部分接口是需要用户授权同意后才能调用的。 像?
所以? app.json{
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
}
复制代码
获取经纬度信息第二步?在需要使用到? 使用? success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
}
})
复制代码
所以我们可以在? onLoad: function() {
// 将当前页面的 this 赋值给 vm,以区别于下面回调函数中的 this
const vm = this
wx.getSetting({
success(res) {
// 1. scope.userLocation 为真, 代表用户已经授权
if (res.authSetting['scope.userLocation']) {
// 1.1 使用 getlocation 获取用户 经纬度位置
wx.getLocation({
success(res){
// 1.2 获取用户位置成功后,将会返回 latitude,longitude 两个字段,代表用户的经纬度位置
console.log(res)
// 1.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址
vm.getAddress(res.latitude,res.longitude)
}
})
}else {
// 2. 用户未授权的情况下, 打开授权界面, 引导用户授权.
wx.openSetting({
success(res) {
// 2.1 如果二次授权允许了 userLocation 权限, 就再次执行获取位置的接口
"scope.userLocation"]) {
wx.getLocation({
success(res){
// 2.2 获取用户位置成功后,将会返回 latitude,longitude 两个字段,代表用户的经纬度位置
console.res)
// 2.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址
res.longitude)
}
})
}
}
})
}
}
})
}
复制代码
上面的代码的逻辑是: 当页面加载完成后, 1. 先获取用户授权列表。 并判断是否有?
注意 注意 注意:? 转化经纬度信息为?
|