How to obtain microbused microfinder user position permissions (reminded)

WeChat applet has three ways to obtain the current location of the user:

1. WX.GetLocation with wx.openLocation)

Gets current accuracy, latitude, speed. No authorization is required. When Type is set to GCJ02, returning to the coordinate

2. Wx.chooselocation

An authorization is required to open the map selection position



if the permissions are allowed For the first time, it is not allowed to call the FAIL method

微信小程序用户位置权限的获取方法(拒绝后提醒) to be authorized to use the WeChat built-in map to view the location. How many of the routes for viewing the start point to the end

There are three types:

1. wx.get setting

Obtaining the current setting of the user, only the apparatus has been requested to request the user in the return value, similar to the sample


2. WX.OpenSetting

Tuning the Permissions Settings Select interface, the setting interface will only have permissions to the user, similar to the following look


3. WX.Authorize

initiate an authorization request to the user in advance. After calling, you will immediately pop up the window asking if the user agrees to authorize the applet to use a certain work.Or you can obtain some data users, but does not actually call the corresponding interface. If you have consent, does not popups appear directly before the user returns success. , Might look like


This is what wx.authorize appears

the question is: If I were the first time wx.chooseLocation () get permission denied, then use the wx.getSetting ()

to reacquire the rights of the how to do it?

thinking: wx.chooseLocation () There are methods fail, if after the first refusal, subsequent calls to select the map are triggered by this, then I can be in there fail method, using wx.getSetting ()

, so that every time it is determined whether a given permission.

The first step: Because there are multiple possible ways of using the positioning will be, so I wrote App.js positioning method, the easy call
   App ({// get the user's geolocation permissions getPermission: function (obj) {wx.chooseLocation ({success: function (res) {obj.setData ({addr: res.address // successfully invoked directly address})} , fail: function () {wx.getSetting ({success: function (res) {var statu = res.authSetting; if (statu [ 'scope.userLocation']) {wx.showModal ({title:! 'whether the current authorization Location',Content: 'Need to get your location, please confirm the authorization, otherwise the map function will not be available', success: function (tip) {wx.openSetting ({Success: function (data) {ix ({Success: function (data) {ix {i Data.authSetting ["Scope.userLocation"] === True) {wx.showtoast ({Title: 'Authorized Success', Icon: 'Success', Duration: 1000} // Remove the Chooselocation Selection Place WX.chooselocation ({success) {Obj.SetData ({addr: res.address})}},})} else {wx.showtoast ({Title: 'authorized failed ", icon:' Success', DURATION : 1000})}}}}}}}}}}, fail: function (res) {wx.showtoast ({Title: 'Call Authorization Window Failed ", icon:' Success', Duration: 1000})}}})}}}}}}}}) 

In the second step: in the page that needs to get the address:

VAR App = getApp (); Page ({DATA : {Addr: ‘Please select location’}, // Select Get Geographic Location GetDress: Function () {var That; App.getPermission (That); // Incoming That value can be directly set directly to the app.js page },})
   Final rendering: 

The position of the last position acquired on the phone is not large .

——————————————– —————————————- 微信小程序用户位置权限的获取方法(拒绝后提醒)

Update WX.OpenSetting precautions.

After the version 2.3.0, after the user has a click behavior, it can jump open the setting page, and the authorization information

is 2.3.0, I passed the above WX.ShowModal. The callback function calls wx.openSetting will happen the following error:

OpenSetting: Fail Can Only Be Invoked by UserTAP GESTURE.

But I test 2.2.4 until 2.3.1 will appear above.

2.3.2 and above will not have this problem. . . . . . . .

And when I test 2.0.8 to 2.2.3, the following error occurs. . . . . .

There is no such problem. Do not understand. . .


The above is the acquisition method of the WeChat applet user position permission to introduce you, I hope to everyone Help, if you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment