微信小程序-關於獲取當前位置用戶授權

wx.getLocation(Object object)

獲取當前的地理位置、速度。當用戶離開小程序後,此接口無法調用。開啓高精度定位,接口耗時會增加,可指定 highAccuracyExpireTime 作爲超時時間。

wx.getLocation({
 type: 'wgs84',//wgs84 返回 gps 座標,gcj02 返回可用於 wx.openLocation 的座標
 success (res) {
   const latitude = res.latitude
   const longitude = res.longitude
   const speed = res.speed
   const accuracy = res.accuracy
 }
})

執行上述代碼,就會出現如下彈框:
在這裏插入圖片描述

wx.getSetting(Object object)

獲取用戶的當前設置。返回值中只會出現小程序已經向用戶請求過的權限。

wx.getSetting({
  withSubscriptions: true, //是否同時獲取用戶訂閱消息的訂閱狀態,默認不獲取
  success (res) {
    console.log(res.authSetting)
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.subscribeMessage": true
    // }
    console.log(res.subscriptionsSetting)
    // res.subscriptionsSetting = {
    //   SYS_MSG_TYPE_INTERACTIVE: 'accept',
    //   SYS_MSG_TYPE_RANK: 'accept',
    //   zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject',
    //   ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
    // }
  }
})

注:如果沒有優先執行wx.getLocation(Object object),res.authSetting[‘scope.userLocation’]的結果爲undefined。如果獲取當前地理位置res.authSetting[‘scope.userLocation’]的結果爲true,否則爲false

wx.openSetting(Object object)

調起客戶端小程序設置界面,返回用戶設置的操作結果。設置界面只會出現小程序已經向用戶請求過的權限。
在這裏插入圖片描述
注:在執行wx.getLocation(Object object)方法後,沒有獲取當前地理位置,還需獲取地理位置時,需要先調起設置界面,如果沒有執行過wx.getLocation(Object object)方法,上圖“設置”界面將不會顯示“是用我的地理位置”按鈕。

總結:

小程序獲取當前用戶地理位置時,必須先執行一次wx.getLocation(Object object)方法,這樣wx.getSetting(Object object)方法獲取res.authSetting[‘scope.userLocation’]的結果不是true就是false。如果沒有執行過wx.getLocation(Object object)方法,res.authSetting[‘scope.userLocation’]的結果爲undefined。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章