鏈接:https://blog.csdn.net/hongzhangzhao/article/details/79427668
微信小程序提供的接口只能獲得用戶的經緯度
wx.getLocation(Object)
可以通過騰訊提供的位置位置服務接口解析經緯度來獲取地址信息
首先進入騰訊位置服務官網http://lbs.qq.com/index.html
在右上角選擇"接入指引"
~ 完成圖中前三個步驟就Ok了,主要是獲得開發密鑰(Key)
~ 接下來在首頁,選擇"開發"下的"WebService API",進入里面有接口相關的使用信息
~ 下面是代碼展示
wx.getLocation({ //沒有特別說明的都是固定寫法 type: 'wgs84', success: function (res) { console.log('location', res); var locationString = res.latitude + "," + res.longitude; wx.request({ url: 'http://apis.map.qq.com/ws/geocoder/v1/', data: { "key": "xxxx-xxxx-xxxx-xxxx-xxxx-xxxx", "location": locationString }, method: 'GET', success: function (r) { //輸出一下位置信息 console.log('用戶位置信息',r.data.result.address); //r.data.result.address獲得的就是用戶的位置信息,將它保存到一個全局變量上 getApp().globalData.locationInfo = r.data.result.address; //這步是將位置信息保存到本地緩存中,key = value的形式 try { wx.setStorageSync('locationInfo', r.data.result.address) } catch (e) { console.log(e) } } }); } });
key:3KQBZ-UPFWP-IGDDH-LP4OP-XM4C3-2KBBB
key地址:https://lbs.qq.com/console/setting.html?key=3KQBZ-UPFWP-IGDDH-LP4OP-XM4C3-2KBBB