Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report