小程序取消地理位置授權后,重新授權


首次進入該頁面,調用wx.getLocation要求用戶進行授權;用戶拒絕后,再次進入該頁面,我們通過wx.getSetting接口,返回用戶授權的情況:

 

getLocation: function () {
    var that = this;
    //1、獲取當前位置坐標
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.setStorageSync('latitude', res.latitude);
        wx.setStorageSync('longitude', res.longitude);
      }
    })
  },

1.展現步驟---取消獲取你的地理位置

2.再次彈出是否授權當前位置彈窗

3.授權所彈出界面

 

 

again_getLocation:function(){
    let that = this;
    // 獲取位置信息
    wx.getSetting({
      success: (res) => {
        console.log(res)
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化進入該頁面,且未授權
          wx.showModal({
            title: '是否授權當前位置',
            content: '需要獲取您的地理位置,請確認授權,否則無法獲取您所需數據',
            success: function (res) {
              console.log(res)
              if (res.cancel) {
                that.setData({
                  isshowCIty: false
                })
                wx.showToast({
                  title: '授權失敗',
                  icon: 'success',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    console.log(dataAu)
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授權成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授權,調用getLocationt的API
                      that.getLocation(that);
                    } else {
                      wx.showToast({
                        title: '授權失敗',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化進入
          that.getLocation(that);
        }
        else { //授權后默認加載
          that.getLocation(that);
        }
      }
    })

  },

 


免責聲明!

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



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