当前位置:网站首页>Wechat applet obtains the current location (startlocationupdate, onlocationchange, offlocationchange)
Wechat applet obtains the current location (startlocationupdate, onlocationchange, offlocationchange)
2022-06-13 02:52:00 【Programming Bruce Lee】
Wechat applet gets the current location
problem
I've been using it wx.getLocation() Method to get the user's location , Discovery is always a matter of chance , Our call frequency is once per second , When doing other business, it will also call , Refer to official documents and find Location document This approach is limited , Wechat restrictions 30 Only one success per second , Other returns fail .
resolvent
Use startLocationUpdate、onLocationChange、offLocationChange Use the three methods together , It can obtain the user's location in real time , And frequent calls do not fail .
Wrapper utility class
// Underground information location acquisition tool class
/** * Get the current location of the user 【 Adapt to the new version of wechat to obtain the location information in the field , used getLocation Frequent method calls have performance problems , also 30 You can only succeed once per second 】 */
const getLocation = () => {
return new Promise((resolve, reject) => {
let _locationChangeFn = (res) => {
resolve(res) // Send back the location information in the field
wx.offLocationChange(_locationChangeFn) // Turn off real-time positioning
wx.stopLocationUpdate(_locationChangeFn); // lsnrctl stop If you don't turn off listening , Sometimes it's very slow to get the location
}
wx.startLocationUpdate({
success: (res) => {
wx.onLocationChange(_locationChangeFn)
},
fail: (err) => {
// Regain location permissions
wx.openSetting({
success(res) {
res.authSetting = {
"scope.userLocation": true
}
}
})
reject(err)
}
})
})
}
module.exports = {
getLocation
}
call
const locationUtils = require('../../../utils/location') // Introduce tool classes
// Get current location
locationUtils.getLocation().then(res => {
console.log(' The latitude and longitude of the current position is :')
console.log(res.latitude,res.longitude)
})
边栏推荐
- OpenCVSharpSample04WinForms
- CV 06 demonstrates backgroundworker
- For loop instead of while loop - for loop instead of while loop
- PK of dotnet architecture
- [reading papers] deep learning face representation from predicting 10000 classes. deepID
- Summary of innovative ideas of transformer model in CV
- Simple use of leaflet - offline map scheme
- Ijkplayer source code - audio playback
- Implementing fillet in custom dialog
- Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
猜你喜欢

Linked list: palindrome linked list
![[data analysis and visualization] key points of data drawing 9- color selection](/img/76/0d707b3d2446b5cd8002cbc2834c5c.jpg)
[data analysis and visualization] key points of data drawing 9- color selection

Rough understanding of wechat cloud development

A real-time target detection model Yolo

Summary of innovative ideas of transformer model in CV
![[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion](/img/b7/0171a637d00e979fb92b82d0c038be.jpg)
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion

Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)

04 route jump and carry parameters

Data warehouse notes | 5 factors that need attention for customer dimension modeling
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
随机推荐
Prometheus node_ Exporter installs and registers as a service
Ijkplayer source code - audio playback
[thoughts in the essay] mourn for development technology expert Mao Xingyun
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
Opencv 07, pixel read, change and bitmap write
Mp4 playback
Techniques for collecting stringgrid
Ijkplayer source code ---setdatasource
Linked list: the first coincident node of two linked lists
Opencv 15 face recognition and eye recognition
Welcome to blog navigation
[common tools] pyautogui tutorial
Delphi implements adding a column of serial number to the CXGRID list
Radio design and implementation in IVI system
二叉树初始化代码
Opencv 9 resize size change rotate rotate blur mean (blur)
Linked list: palindrome linked list
Professional database management software: Valentina Studio Pro for Mac
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
Ijkplayer source code - choose soft decoding or hard decoding