乐趣区

关于前端:微信小程序地理位置授权

文档

  • 微信小程序受权文档
  • 获取地理位置文档

代码

wx.getSetting({success(res) {console.log('wx.getSetting:', res)
        /** 
         * res.authSetting['scope.userLocation'] === true         批准过受权
         * res.authSetting['scope.userLocation'] === false        回绝过受权
         * res.authSetting['scope.userLocation'] === undefined    从未批准或者回绝过受权
        */
        if (res.authSetting['scope.userLocation'] === undefined) {
          wx.authorize({
            scope: 'scope.userLocation',
            success (res) {console.log('authorize success:', res)
            },
          })
        }else if (res.authSetting['scope.userLocation'] === false) {
          wx.showModal({
            title: '',
            content: '去小程序设置页面设置 balabala',
            showCancel: true,
            cancelText: '勾销',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {if(result.confirm){
                wx.openSetting({success (res) {console.log('wx.openSetting success:', res.authSetting)
                  },
                  fail: (err)=>{console.log('wx.openSetting fail:', err)
                  },
                  complete: (info)=>{console.log('wx.openSetting complete:', info)
                  },
                })
              }
            },
          })
        } else {
          wx.getLocation({
            type: 'wgs84',
            success (res) {console.log('wx.location success:', res)
            },
            fail (err) {console.log('wx.location fail:', err)
            },
            complete(aa) {console.log('wx.location complete:', aa)
            },
          })
        }
      },
    })

效果图

受权弹窗

去设置弹窗

注意事项

1、wx.openSetting

- 3.0 版本开始,用户产生点击行为后,才能够跳转关上设置页,治理受权信息
- 真机调试能力关上设置页面
退出移动版