Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of re-authorization when WeChat Mini Programs appears wx.getLocation

Shulou Source: shulou.com Published: 2022-05-31 21:14:44 09月21日 Update

This article mainly explains "WeChat Mini Programs appears wx.getLocation re-authorization how to solve", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "WeChat Mini Programs appears wx.getLocation re-authorization how to solve it"!

First, define a common function outside the page to send a request for a location

Var getLocation = function (that) {wx.getLocation ({type: 'wgs84', success: function (res) {/ / Latitude and longitude var latitude = res.latitude var longitude = res.longitude var aK = that.data.aK wx.request ({url:' https://api.map.baidu.com/geocoder/v2/?ak=' + aK +'& location=' + latitude +','+ longitude +'& output=json', data: {} Header: {'content-type':' application/json'}, success: function (res) {var city = res.data.result.addressComponent.city That.setData ({currentCity: city}) wx.request ({url: 'xxx' + city, data: {}, header: {' content-type': 'application/json'}, success: function (res) {that.setData ({county: res.data,})},})} Fail: function () {wx.showToast ({title: 'authorization failed', icon: 'success', duration: 1000})}

Then, you need a location in page to call the getLocation function outside of page

Wx.getSetting ({success: (res) = > {if (res.authSetting ['scope.userLocation']! = undefined & & res.authSetting [' scope.userLocation']! = true) {/ / enter the page without initialization and do not authorize wx.showModal ({title: 'whether to authorize the current location', content: 'need to obtain your geographic location, please confirm the authorization Otherwise, you can't get the data you need', success: function (res) {if (res.cancel) {that.setData ({isshowCIty: false}) wx.showToast ({title: 'authorization failed', icon: 'success' Duration: 1000})} else if (res.confirm) {wx.openSetting ({success: function (dataAu) {if (dataAu.authSetting ["scope.userLocation"] = = true) {wx.showToast ({title: 'authorization successful', icon: 'success' Duration: 1000}) / / re-authorization Call API getLocation (that) of getLocationt } else {wx.showToast ({title: 'authorization failed', icon: 'success' Duration: 1000})} else if (res.authSetting ['scope.userLocation'] = = undefined) {/ / initialize into getLocation (that) } else {/ / load getLocation (that) by default after authorization;})

The above procedures are executed in the following order:

1. Load wx.getLocation first and pop up your own authorization box, as shown in the figure

Then, click OK to authorize. If you click cancel, cancel the authorization. When authorization is needed again, our custom Modal box will be called, as shown in the figure.

Secondly, click cancel to close the above Modal box, and click OK to open the address authorization settings of the phone, as shown in the figure.

Finally, if you directly click the return arrow at the top left, you will cancel the authorization. If you select the geolocation button first, and then click the return arrow at the top left, the authorization will be successful, as shown in the figure.

At this point, I believe that everyone has a deeper understanding of "WeChat Mini Programs appears wx.getLocation re-authorization how to solve", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Again location such as figure program success content function geography location arrowhead learning practical deeper interest address practicality practical mobile phone button easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information vpn Huawei NVidia