


How does the mini program obtain the user's current location?
Jan 29, 2021 am 09:58 AMThere are three ways to obtain the user’s current location:
(Learning video sharing: Introduction to Programming)
1. wx.getLocation (mostly used together with wx.openLocation)
Get the current accuracy, latitude, and speed. No authorization is required. When type is set to gcj02, return the coordinates that can be used for wx.openLocation
2, wx.chooseLocation
Requires authorization, open the map to select the location
When calling the method for the first time
appears and then
appears after permission is allowed. If it is not allowed the first time, wx will be called continuously. The fail method of chooseLocation
3, wx.openLocation
requires authorization and uses WeChat’s built-in map to view the location. Mostly used to check the route from the starting point to the end point
There are three authorization methods:
1, wx.getSetting
Get the user The current settings, only the permissions that the applet has requested from the user will appear in the return value, similar to the following
2, wx.openSetting
Call up the permission setting selection interface. Only the permissions that the mini program has requested from the user will appear in the setting interface, similar to the following
3, wx.authorize
Initiate an authorization request to the user in advance. After the call is made, a pop-up window will immediately ask the user whether they agree to authorize the applet to use a certain function or obtain some of the user's data, but the corresponding interface will not actually be called. If the user has previously agreed to the authorization, no pop-up window will appear and success will be returned directly. , similar to the following
This is what wx.authorize appears
The question is: If I use wx.chooseLocation() for the first time to obtain What should I do if permission is denied and then use wx.getSetting() to regain permission?
Idea: wx.chooseLocation() has a fail method. If after the first rejection, subsequent calls to select the map will trigger this, then I can use wx.getSetting() in the fail method, like this You can determine whether permission has been granted every time.
The first step: Since the positioning method may be used multiple times, I wrote the positioning method into App.js to facilitate the call
App({ //獲取用戶地理位置權(quán)限 getPermission:function(obj){ wx.chooseLocation({ success: function (res) { obj.setData({ addr: res.address //調(diào)用成功直接設(shè)置地址 }) }, fail:function(){ wx.getSetting({ success: function (res) { var statu = res.authSetting; if (!statu['scope.userLocation']) { wx.showModal({ title: '是否授權(quán)當(dāng)前位置', content: '需要獲取您的地理位置,請(qǐng)確認(rèn)授權(quán),否則地圖功能將無法使用', success: function (tip) { if (tip.confirm) { wx.openSetting({ success: function (data) { if (data.authSetting["scope.userLocation"] === true) { wx.showToast({ title: '授權(quán)成功', icon: 'success', duration: 1000 }) //授權(quán)成功之后,再調(diào)用chooseLocation選擇地方 wx.chooseLocation({ success: function(res) { obj.setData({ addr: res.address }) }, }) } else { wx.showToast({ title: '授權(quán)失敗', icon: 'success', duration: 1000 }) } } }) } } }) } }, fail: function (res) { wx.showToast({ title: '調(diào)用授權(quán)窗口失敗', icon: 'success', duration: 1000 }) } }) } }) },})
The second step: Get the positioning method when needed In the address page:
var app = getApp(); Page({ data:{ addr:'請(qǐng)選擇位置' }, //選擇獲取地理位置 getAddress:function(){ var that=this; app.getPermission(that); //傳入that值可以在app.js頁(yè)面直接設(shè)置內(nèi)容 }, })
Final rendering:
The final location deviation obtained on the mobile phone is not too large.
Notes on updating wx.openSetting.
Starting from version 2.3.0, users can jump to the settings page and manage authorization information only after they click.
That is, after version 2.3.0, I passed the wx.showModal above The following error will occur when the callback function calls wx.openSetting:
openSetting:fail can only be invoked by user TAP gesture.
But I tested 2.2 The above error will appear from .4 to 2.3.1.
2.3.2 and above will not cause this problem. . . . . . . .
And when I test 2.0.8 to 2.2.3 the following error will appear. . . . . .
Others don’t have this problem. Don't understand. . .
Related recommendations: Mini Program Development Tutorial
The above is the detailed content of How does the mini program obtain the user's current location?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

To learn more about the storage location of packages installed by pip, you need specific code examples. Pip is a commonly used package management tool in the Python language. It is used to easily install, upgrade and manage Python packages. When using pip to install a package, it will automatically download the corresponding package file from PyPI (Python Package Index) and install it to the specified location. So, where are the packages installed by pip stored? This is a problem that many Python developers will encounter. This article will delve into the location of the packages installed by pip and provide

There are a total of 20 origami birds in Croaker Film and Television Park on Star Dome Railway. Many players don’t know where the origami birds are in Crocker Film and Television Park. The editor has summarized the locations of each origami bird to help everyone. Search for it, and take a look at this latest summary of the locations of the origami birds in Croaker Film and Television Park for specific content. Guide to the Honkai Star Dome Railway: Origami Bird in Crook Movie Park Location 1, Crook Movie Park 1st Floor 2, and Crook Movie Park 2nd Floor Star Dome Railway

If we are going to be away from the computer for a long time, it is best to shut down the computer to protect it. So where is the shutdown in win11? In fact, generally speaking, just open the start menu and you can find the shutdown button in it. Where to shut down Windows 11: Answer: In the power button of the start menu. 1. First, we click the "Windows Logo" on the bottom taskbar to open the "Start Menu" 2. After opening, you can find the "Power" button in the lower right corner, as shown in the figure. 3. After clicking the power button, you will see "Shutdown", click it to shut down. 4. If the computer cannot be shut down due to special circumstances such as a crash, you can directly press and hold the "power button" on the computer to force a shutdown.

1. We open Meituan on the mobile phone, and then click on the takeout option in the upper left corner of the homepage. 2. After entering the takeout platform page, you can see the section with daily coupons on the homepage, click on it directly. 3. After entering the Tiantian God Voucher, you will see a lot of activities, click Finish, and then we can get rewards after completing the tasks.

Players can challenge different bosses in Elden's Ring. Many players don't know where Gretel, the Omen King, is. The Omen King is in the Queen's boudoir. Players can jump from the initial King's boss room to the branches. You can come to the Queen's Boudoir. Where is King Gretel's location in Elden's Ring of Omens? Answer: The Queen's Boudoir. 1. The King of Bad Omens is in the queen's boudoir. 2. Players can come to the queen's boudoir by jumping on the branches from the initial king's boss room. 3. After arriving at your destination, enter through the gate shown in the picture below. 4. After passing through the gate, find the fog location shown in the picture below. Through the fog, you can find the Omen King Mengt,
