


How does the mini program obtain the user's current location?
There 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({ //获取用户地理位置权限 getPermission:function(obj){ wx.chooseLocation({ success: function (res) { obj.setData({ addr: res.address //调用成功直接设置地址 }) }, fail:function(){ wx.getSetting({ success: function (res) { var statu = res.authSetting; if (!statu['scope.userLocation']) { wx.showModal({ title: '是否授权当前位置', content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用', success: function (tip) { if (tip.confirm) { wx.openSetting({ success: function (data) { if (data.authSetting["scope.userLocation"] === true) { wx.showToast({ title: '授权成功', icon: 'success', duration: 1000 }) //授权成功之后,再调用chooseLocation选择地方 wx.chooseLocation({ success: function(res) { obj.setData({ addr: res.address }) }, }) } else { wx.showToast({ title: '授权失败', icon: 'success', duration: 1000 }) } } }) } } }) } }, fail: function (res) { wx.showToast({ title: '调用授权窗口失败', icon: 'success', duration: 1000 }) } }) } }) },})
The second step: Get the positioning method when needed In the address page:
var app = getApp(); Page({ data:{ addr:'请选择位置' }, //选择获取地理位置 getAddress:function(){ var that=this; app.getPermission(that); //传入that值可以在app.js页面直接设置内容 }, })
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

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics

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"

Every Windows system has a startup path. If you add files or software to it, it will be opened at boot time. However, many friends don’t know where the win11 startup path is. In fact, we only need to enter the corresponding folder on the C drive. Win11 startup path: 1. Double-click to open "This PC" 2. Directly paste the path "C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Startup" into the path box. 3. Here is the win11 startup path. If we want to open the file after booting, we can put the file in. 4. If you cannot enter according to this path, it may be hidden.

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

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

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

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.

In "Last Age", players can play in various modes such as game mode, challenge mode, and arena, etc. Arena is the ultimate way to play the game, providing two modes for players to choose from. Where is the Arena in the Last Era? Answer: The Arena is an endgame game, and its specific location is at the Champion's Gate. You need to obtain the Arena Key or Memory Arena Key. After right-clicking, you can see the world map and find the specific location of the Champion's Gate. The arena is divided into two major modes: Arena Championship Mode and Endless Arena Mode. The former includes 40 waves of enemies and selected rewards, always culminating in a battle with the Arena Champion. There are 4 stages in Arena Championship Mode. The higher the difficulty, the better the rewards. Endless Arena is a mode with infinite waves. The difficulty gradually increases. The challenger with the best score will
