1. 程式人生 > >微信小程序之地理位置授權 wx.getLocation

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

user 微信 cat ucc get 獲取 .get ons 通過

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)
        }
    })
},

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