微信小程序之地理位置授权 wx.getLocation

1. 授权地理位置

  • 点击按钮,弹出授权弹窗,点击允许后,在以后的操作中可以随时获取到用户地理位置
  • 点击拒绝后,将无法获取到地理位置,也无法再次点击弹出弹窗。
<button bindtap='onAuthLocation' >授权位置</button>
onAuthLocation() {
    wx.authorize({
        scope: 'scope.userLocation',
        success: (res) => {
            console.log('成功:' , res)
        },
        fail: (res) => {
            console.log('失败:', res)
        },
    })
},

2. 获取地理位置

  • 如果用户之前已经授权地理位置,那么可以通过如下方法获取到地理位置信息
<button bindtap='onGetLocation' >获取位置</button>
onGetLocation() {
    wx.getLocation({ 
        success: (res) => {
            console.log('成功:', res)
        },
        fail: (res) => {
            console.log('失败:', res)
        },
    })
},

3. 拒绝后再次授权,打开授权面板

  • 如果用户有过拒绝授权地理位置的操作,是无法再次打开弹窗授权的,只能通过以下方法,打开所有授权信息的控制面板,进行再次授权
  • 对于已经允许授权的信息,如果用户想拒绝使用,也可通过下面方法,取消授权
<button bindtap='gotoSetting' >打开授权信息面板</button>
gotoSetting() {
    wx.openSetting({
        success: (res) => {
            console.log(res)
        }
    })
},

猜你喜欢

转载自www.cnblogs.com/cckui/p/10002435.html
今日推荐