Because uniapp website document positioning function, can only provide the longitude and latitude, if you want to get the current position of specific information, must want to obtain the official map method, and then click on the map, this violated, open position can be automatically obtained after the current position of provinces and municipalities and counties specific location, so how to get the latitude and longitude, to get the current location? The detailed code is as follows:
Step 1: Apply for your own secret key on Tencent platform (application address:Lbs.qq.com/console/myk…
Step 2: In the background of the small program, configure apis.map.qq.com as a legitimate domain name. Otherwise, apis.map.qq.com will be prompted: apis.map.qq.com is not in the list of legitimate domain names of the following request.
Step 3: Download Tencent Map API JS file:
Link: pan.baidu.com/s/1GS9RuaXp…
Extraction code: 43Jg
Step 4: Place this file in the appropriate folder, such as the common directory
Step 5: Import the JS file
import QQMapWX from '.. /.. /common/qqmap-wx-jssdk'
Copy the code
Step 6: Register in onLoad
OnLoad (option) {this.qqmapsdk = new QQMapWX({//'secret key'})},Copy the code
Step 7: Get the current latitude and longitude
getPreciseLocation: function () {
let vm = this;
uni.getLocation({
type: 'wgs84',
success: function (res) {
var latitude = res.latitude
var longitude = res.longitude
vm.getLocal(latitude, longitude)
},
fail: function (res) {
console.log('fail' + JSON.stringify(res))
}
})
}
Copy the code
The last step is to call Tencent map API to obtain specific provinces and counties according to latitude and longitude
getLocal: function (latitude, longitude) {
let vm = this
vm.qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
console.log(res)
},
fail: function (res) {
console.log(res)
},
complete: function (res) {
// console.log(res)
}
})
}
Copy the code
Note: If the request source is not authorized, it is because the Whitelist of WebServiceAPI domain name cannot be configured when the wechat applet is used. Otherwise, an error will be reported. The solution is to clear the whitelist, as shown in the figure below:
Configuration path: generated address for the key: lbs.qq.com/console/myk…