微信小程序之地理位置授權 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)
        }
    })
},


免責聲明!

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



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