background
Increasingly small program requirements, there will inevitably be different kinds of authorization problems. Today we will learn how to play small program location authorization
Classification of location authorization issues
- Don’t have to allow authorization: Allow the user to continue after the first user decision (not entirely dependent on location information)
- Authorization must be allowed: Only authorization can be used (wX applets that depend on location information)
Classification analysis
1. In the case of requirement classification 1, we won’t say much here, just call wx.getLocation
2. Continued use only under authorization
- App: Used to describe the overall program. You need to add location authorization to the application. The code is as follows
App({ config: { 'permission': { 'scope.userLocation': { 'desc': 'Please enable location authorization' // Highway driving continues background positioning}}}})Copy the code
- Page: A Page or area that processes location information
- Wx. getLocation: small program API, use method and implementation effect as follows
Page({ onLoad () { wx.getLocation({ success: (res) = > {}, // Successful callback fail: (err) = > {} // Failed callback})}})Copy the code
-
$invoke(‘LocationAuthModal’, showModal): By directly calling a method in the LocationAuthModal component, you pop up a prompt window on the page that looks like this. (The LocationAuthModal component is our custom popover and referenced in the page.)
-
Button(open-type= “openSetting” bindOpenSetting =callback): From the 2.3.0 version of the basic library of small programs, users can jump to open the setting page and manage authorization information only after clicking. After the setup is complete, the authorized Auth is retrieved in callback for processing:
<button open-type="openSetting" @opensetting="callback">Copy the code
-
Callback: Callback function after setting
-
Setting: The setting of the applet
-
Auth: authorization information obtained after the configuration is complete
-
Auth [” scope.userLocation “] = true: enables location authorization and can be accessed directly from wx.getLocation
-
$invoke(‘LocationAuthModal’, showModal) is invoked to forcibly remind the user to enable location authorization
-
SuceeCallback: callback function after successfully obtaining location, used to deal with the operation after obtaining wechat knowledge
instructions
- Part of the code in the document adopts wepy, the small program framework of wechat
- Development process using small program base library version for V2.10.1
Recommended links
- Wx. GetLocation use
- Wx. OpenSetting use
- The use of button components
- Article source demo
- How to play small program login system