微信小程式之地理位置授權 wx.getLocation
阿新 • • 發佈:2018-11-22
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) } }) },