要想通过微信小程序获得用户的准确位置,大体的步骤就两步,由于微信小程序只能获取到经纬度,所有需要用到第三方的API来反查询用户的地理位置,具体步骤如下:
1、获取经纬度
wx.getLocation({
success: function (res) {
console.log(res)
//保存到data里面的location里面
that.setData({
location: {
longitude: res.longitude,
latitude: res.latitude
}
})
}
})
2、根据经纬度取得位置信息
这一步需要用到第三方的api,这里我选择用腾讯自己家的地图,去他的官网
http://lbs.qq.com
注册下,就可以了,不需要认证啥的。
![](https://img.haomeiwen.com/i2275747/6cb9c37749c0f701.png)
然后就可以使用了,代码如下:
var qqMapApi = 'http://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + that.data.location.latitude + ',' +
that.data.location.longitude + "&key=你的腾讯地图的key" + "&get_poi=1";
wx.request({
url: qqMapApi,
data: {},
method: 'GET',
success: (res) => {
console.log(res.data)
//取位置名
that.setData({
loca: res.data.result.address
})
}
});
[获取授权]
网友评论