小程序判斷用戶是否授權位置信息


今天記錄一下小程序中,用戶是否授權位置信息的問題:

場景:當用戶點擊某個按鈕的時候,需要先獲取到用戶的位置,然后再進行其他操作;果如用戶拒絕了授權,當再次點擊按鈕的時候依然會彈起授權窗口;

 1 // index/index.js
 2 Page({
 3     data: {
 4         latitude: '',    //緯度
 5         longitude: '',  //經度
 6         address: '',    //地址
 7     },
 8 
 9   /**
10    * 點擊打卡
11    */
12   signInClick(e) {
13       // 判斷用戶是否授權,里面會調用打卡接口
14       this.isGetSetting()
15   },
16 
17       /**
18    * 判斷用戶是否授權
19    */
20   isGetSetting() {
21     // 判斷用戶是否授權
22     wx.getSetting({
23       success: (res) => {
24         var statu = res.authSetting;
25         if (!statu['scope.userLocation']) { //沒授權
26           wx.showModal({
27             title: '是否授權當前位置',
28             content: '需要獲取您的地理位置,請確認授權',
29             confirmColor: '#f16765',
30             success: res => {
31               if (res.confirm) {
32                 wx.openSetting({
33                   success: data => {
34                     if (data.authSetting["scope.userLocation"]) {
35                       // 調用百度地圖,獲取位置信息(就是經緯度轉換成地址)
36                       this.bdMap()
37                     }
38                   }
39                 })
40               }
41             }
42           })
43         } else { //已授權
44             //做一些事情...
45             
46         }
47       }
48     })
49   },
50 
51   /**
52    * 百度API封裝,用於獲取用戶位置
    * 如果要使用百度地圖獲取具體位置,申請完key以后,需要在小程序后台配置百度域名https://api.map.baidu.com
53 */ 54 bdMap() { 55 // 調用百度地圖api 56 var Bmap = new bmap.BMapWX({ 57 ak: 'IhgcYGTwAWc2xxxxxxxxxxxxx' //這個ak需要在百度地圖開放平台根據小程序的AppID申請 58 }); 59 // 進行regeocoding檢索,根據經緯度獲得對應的地理描述信息 60 Bmap.regeocoding({ 61 success: res => { 62 var _data = res.wxMarkerData; 63 this.setData({ 64 latitude: _data[0].latitude, 65 longitude: _data[0].longitude, 66 address: _data[0].address + _data[0].desc, 67 }) 68 // 做一些事情.... 69 70 }, 71 fail: res => { 72 wx.showToast({ 73 title: '您拒絕了授權,XX功能將無法使用', 74 icon: 'none', 75 duration: 3000 76 }) 77 } 78 }) 79 }, 80 81 })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM