小程序獲取地理位置授權


效果圖: 用戶拒絕授權,將繼續提醒用戶繼續授權,否則不能使用

  = =

 

 

注釋:從今年2019起,地理位置授權  需要標明  用處,不然真機調試  顯示不了授權:

在app.json中添加描述:

 "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息將用於定位"
    }
  },
  "sitemapLocation": "sitemap.json"

 

wx.getLocation({
      success(res) {
        that.setData({
          currentLon: res.longitude,
          currentLat: res.latitude,
        });
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授權當前位置',
                content: '需要獲取您的地理位置,請確認授權,否則地圖功能將無法使用',
                success: function (tip) {
                  console.log(1)
                  if (tip.confirm) {
                    console.log(1)
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授權成功',
                            icon: 'success',
                            duration: 1000
                          })
                          wx.getLocation({
                            success(res) {
                              that.setData({
                                currentLon: res.longitude,
                                currentLat: res.latitude,
                              });
                            },
                          });
                        } else {
                          wx.showToast({
                            title: '授權失敗',
                            icon: 'success',
                            duration: 1000
                          })
                          wx.navigateBack({
                            delta: -1
                          });
                        }
                      }
                    })
                  }else{
                    wx.navigateBack({
                      delta: -1
                    });
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '調用授權窗口失敗',
              icon: 'success',
              duration: 1000
            })
            wx.navigateBack({
              delta: -1
            });
          }
        })
      }
    })

 


免責聲明!

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



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