Sow nothing reap nothing

微信小程序获取地址位置授权及用户拒绝后处理方式

已有8,663次关注

1、微信小程序获取地理位置授权,首先需要在app.json中添加配置:

"permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序定位,以便更好体验功能"
    }
}

2、wxml代码:

<view class="map-point-select" bindtap="mapSetting">获取地址信息</view>

3、js代码:

/**
* 点击获取授权
*/
mapSetting: function () {
    let that = this;
    wx.getSetting({
      success: (res) => {
        // 拒绝授权后再次进入重新授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '',
            content: '我们需要获取你的地理位置,请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '您已拒绝授权',
                  icon: 'none'
                })
                setTimeout(() => {
                  wx.navigateBack();
                }, 1500)
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      that.getLocation(dataAu);
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none'
                      })
                      setTimeout(() => {
                        wx.navigateBack();
                      }, 1500)
                    }
                  }
                })
              }
            }
          })
        }
        // 初始化进入,未授权
        else if (res.authSetting['scope.userLocation'] == undefined) {
          that.getLocation(res);
        }
        // 已授权
        else if (res.authSetting['scope.userLocation']) {
          that.getLocation(res);
        }
      }
    });
},

/**
* 获取地理信息
*/
getLocation: function (userLocation) {
    let that = this;
    wx.getLocation({
      type: "gcj02",
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;
        
        console.log(res);
      },
      fail: function (res) {
        if (res.errMsg === 'getLocation:fail:auth denied') {
          wx.showToast({
            title: '您已拒绝授权',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack();
          }, 1500)
          return
        }
        if (!userLocation || !userLocation.authSetting['scope.userLocation']) {
          vm.mapSetting();
        } else if (userLocation.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '',
            content: '请在系统设置中打开定位服务',
            showCancel: false,
            success: result => {
              if (result.confirm) {
                wx.navigateBack();
              }
            }
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack();
          }, 1500)
        }
      }
    });
},

4、如果设备未开启位置信息服务,授权成功后在wx.getLocation()方法中也会获取失败,需要在fail方法中提示用户开启手机位置信息。

已自动关闭评论