通過微信自己的接口API,用戶授權后獲取到經緯度,通過經緯度調用地圖接口返回地理位置信息。
簡單、明了!!!(網上自己查詢的文檔進行編程,轉載請注明出處)
代碼如下:
qqMapApi: 'http://apis.map.qq.com/ws/geocoder/v1/', //地圖接口鏈接
//獲取經緯度
getPosition() {
let that = this;
wx.getLocation({
type: 'wgs84',
success: function(res) {
var latitude = res.latitude;
var longitude = res.longitude;
// wx.setStorageSync('latitude', latitude) //緯度
// wx.setStorageSync('longitude', longitude) //經度
var qqMapApi = that.qqMapApi + "?location=" + latitude + ',' +
longitude + "&key=" + 'XVLBZ-BSU66-ULJSQ-MFGXD-TM7GZ-55F2M' + "&get_poi=1";
wx.request({
url: qqMapApi,
data: {},
method: 'GET',
success: (res) => {
console.log(res)
if (res.statusCode == 200 && res.data.status == 0) {
that.country = res.data.result.address_component.nation;
that.province = res.data.result.address_component.province;
that.city = res.data.result.address_component.city;
that.county = res.data.result.address_component.district;
that.street = res.data.result.address_component.street;
}
}
})
},
fail() {
that.fn_fail();
}
})
},