1.申请接口权限
先通过类目审核,再在小程序管理后台,「开发」-「开发管理」-「接口设置」中自助开通该接口权限。
2.在app.json中配置
与pages同级:
"requiredPrivateInfos": [
"getLocation"
]
3. 调用接口
wx.getLocation接口返回成功的回调函数中将获取的latitude、longitude作为参数
调用腾讯位置服务(逆地址解析)'https://apis.map.qq.com/ws/geocoder/v1/?location=xxx,xxx&key=xxx'
location(经纬度)和key(开发密钥)为必填
经纬度(GCJ02坐标系),格式:location=lat<纬度>,lng<经度>
var that = this
wx.getLocation({
success(res){
wx.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=请填写开发密钥`,
success: (res) => {
that.setData({
address: res.data.result.address,
recommend: res.data.result.formatted_addresses.recommend,
})
}
})
},fail(err){
console.log('错误信息',err);
}
})