Wechat applet positioning to obtain national provinces and municipalities

It can be seen from the documentation that this method only returns the location coordinates and other information, and does not return the geographic location name. Fortunately, Tencent Maps provides an interface SDK for small programs to obtain location information.

The document address is as follows: http://lbs.qq.com/qqmap_wx_jssdk/index.html 
For SDK usage, please refer to this document.

The process of obtaining geographic location information in the applet is as follows:

  1. Get the current location coordinates according to the wx.getLocation method.
  2. Obtain the current coordinate geographic location information according to the reverseGeocoder method. 
Note: https://apis.map.qq.com is not a legitimate domain name of the request , the solution: 1: Turn off the verification domain name

 

, 2: Add the request legal domain name to the applet console

 

 
var mapsdk = require("../../utils/qqmap-wx-jssdk.min.js")
 
Page({

/**
* Initial data of the page
*/
data: {
 
},

/**
* Life cycle function--listen to page load
*/
onLoad: function (options) {
var that = this
var wxmapsdk = new mapsdk ({
key: 'Tencent map development key (key)'
});
wx.getLocation({
success: function (res) {
//2. Obtain the name of the current location according to the coordinates and display it at the top: Tencent Map Reverse Address Resolution
console.log(res)
wxmapsdk.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success: function (ress) {
console.log(ress)
var nation = ress.result.address_component.nation;
var province = ress.result.address_component.province;
var city = ress.result.address_component.city;
var district = ress.result.address_component.district;
that.setData({
nation: nation,
province: province,
city: city,
district: district
})
}
})
}
})
}
})

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325240286&siteId=291194637