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!