当前位置:网站首页>Wechat applet (get location)
Wechat applet (get location)
2022-06-13 06:12:00 【Vue sauce】
It is not difficult to locate wechat applets , But if the first contact is still very ignorant , Here is the record , It is convenient for you to review or learn from !
First of all, there are two key points :
- Through wechat applet api: wx.getLocation(Object object) Get the current latitude and longitude ; Applet api details
- Longitude and latitude obtained through reverse address resolution of Tencent map open platform , Get the real landmark name ; Tencent map platform
Specific operation :
( Tencent map operation )
3. Register Tencent map open platform :
4. Apply for developer key (Key): Request key https://lbs.qq.com/dev/console/key/add
A string will be returned after success key, This key We'll use that later 
5. Console -> key management -> Set up ( How to use this function key)-> Check webserviceAPI -> preservation 

( Wechat platform operation )
6. Configure the security domain name WeChat public platform https://mp.weixin.qq.com/
. Log in to your applet on wechat public platform -> Development -> Development and setup -> Server domain name -> take https://apis.map.qq.com fill request Legitimate domain name 
7. Download the applet sdk( Copy the link to open a new tab to download ) http://3gimg.qq.com/lightmap/xcx/jssdk/qqmap-wx-jssdk1.2.zip
8. Parsing compressed folders , Put it into the applet project file , You can also create a new folder to store such files , I put it in the newly built libs Internal
9. Use :
//app.json file
// This is because developers need to fill in the description of how to get the user's geographical location . If it is not configured, you will be prompted to declare
{
"permission": {
"scope.userLocation": {
"desc": " Your location information will be used to show the effect of the applet location interface "
}
}
}
// Introduce... On the page you need to use :
let QQMapWX = require('./libs/qqmap-wx-jssdk')
let qqmapsdk;
getLocation() {
qqmapsdk = new QQMapWX({
key: 'CBAJ-DUD-EURJ-JFFJD' // Previously applied on Tencent platform key
})
wx.getLocation({
// Get the longitude and latitude of the location address
type: 'wgs84',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
qqmapsdk.reverseGeocoder({
//SDK call
location: {
latitude,
longitude
},
success: (res) => {
//success Method points to closure , therefore this Belongs to closure , So in success Callback functions cannot be used directly this.setData() Of
// You can use the outside to this Assign to other variables , Can also pass promise Secondary packaging , Avoid going back to hell
console.log(res)
}
})
}
})
}
My complete implementation :
let QQMapWX = require('./libs/qqmap-wx-jssdk')
let qqmapsdk;
Page({
data: {
location: ' Locate me ...', // Display the positioned data
},
async onShow(options) {
// Get landmarks if authorization is displayed on the implementation page , No authorization , You can only get it by clicking the get button
let res = await wxAuthorize()
if (res) {
this.getLocation()
} else {
this.setData({
location: ' Locate me ...'
})
}
},
async getLocation() {
qqmapsdk = new QQMapWX({
key: 'CBAJ-DUD-EURJ-JFFJD'
})
const res = await new Promise((resolve) => {
wx.getLocation({
// Get address latitude and longitude
type: 'wgs84',
success(res) {
resolve(res);
}
})
})
const latitude = res.latitude
const longitude = res.longitude
const res2 = await new Promise(resolve => {
qqmapsdk.reverseGeocoder({
//SDK The call here only needs to be specific to the city , So these two are enough , Specific information can be obtained according to business requirements
location: {
latitude,
longitude
},
success: (res) => {
resolve(res)
}
})
})
this.setData({
location: res2.result.ad_info.city
})
},
async getData() {
const isAuth = await wxAuthorize();
if (isAuth) {
this.getLocation()
} else {
wx.showModal({
title: ' Please authorize the current location ',
content: ' Need to get your location , Please authorize !!!',
showCancel: true,
confirmText: ' Delegating ',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log(res.authSetting, res, " Promise to authorize ")
this.getLocation()
}
})
}
}
});
}
},
// Tool function , Packaged Promise, Decide whether to authorize , You can avoid hell
async function wxAuthorize(scope = 'scope.userLocation') {
return new Promise(resolve => {
wx.authorize({
scope,// Initiate location authorization
success: ()=> {
console.log(' Have location authorization ')
// Authorized success , Call the get location function here
resolve(true)
},
fail(){
console.log(" Did not agree to authorize the location ")
resolve(false)
}
})
})
}
})
边栏推荐
- After clicking the uniapp e-commerce H5 embedded applet, the page prompts "the page iframe does not support referencing non business domain names"
- Test logiciel - résumé des FAQ d'interface
- The title of the WebView page will be displayed in the top navigation bar of the app. How to customize
- 超有范的 logo 在线设计制作工具
- Leetcode- keyboard line - simple
- CORS request principle
- Wechat applet: use of global state variables
- Echart histogram: echart implements stacked histogram
- Rk3399 hid gadget configuration
- Leetcode- student attendance record i- simple
猜你喜欢
![[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples

Echart柱状图:echart实现堆叠柱状图

1+1>2,Share Creators可以帮助您实现
不在以下合法域名列表中,微信小程序解决办法

Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks

Echart折线图:当多条折线图的name一样时也显示不同的颜色

Annotation only integration SSM framework

Echart line chart: different colors are displayed when the names of multiple line charts are the same

After clicking the uniapp e-commerce H5 embedded applet, the page prompts "the page iframe does not support referencing non business domain names"

微信小程序:点击事件获取当前设备信息(基础)
随机推荐
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
MySQL stored procedure
Echart histogram: X-axis displays value, Y-axis displays category
FusionPBX 安装 —— 筑梦之路
微信小程序:点击事件获取当前设备信息(基础)
自我总结ing
Commit specification
Time conversion'2015-04-20t11:12:00.000+0000 '
Introduction to USB learning (5) -- looking back, the man was in the dim light
【MySQL】基础知识小复习
Uniapp hides the scroll bar of scroll view
Self summarizing
Echart line chart: multiple line charts show only one line at a time
Data conversion analysis tool
MySQL custom function
Swift--function
Leetcode- third largest number - simple
Uni app dynamic setting title
Echart rectangular tree diagram: simple implementation of rectangular tree diagram
[to]12 common IP commands in the iproute installation package