小程序判斷手機位置是否打開

// 獲取當前地地址
    async getLocation() {
      let self = this
      let result = null
      try {
        const resultLocation = await wepy.getLocation()
        result = await this.geocoder(resultLocation)
      } catch (e) {
        wx.getSetting({
          success: function (res) {
            console.log('getSetting...', res)
            let statu = res.authSetting
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否受權當前位置',
                content: '須要獲取您的地理位置,請確認受權,不然將沒法得到優惠券',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        console.log('openSetting...', data)
                        if (data.authSetting['scope.userLocation'] === true) {
                          wepy.showToast({
                            title: '受權成功',
                            icon: 'success',
                            duration: 1000
                          })
                          // 受權成功以後,再調用chooseLocation選擇地方
                          wepy.getLocation({
                            success(res) {
                              self.geocoder(res)
                            }
                          })
                        } else {
                          // 開關關閉,提示沒法拿到位置權限
                          wx.showModal({
                            title: '提示',
                            content: '沒法使用定位權限',
                            confirmText: '太遺憾了',
                            showCancel: false
                          })
                        }
                      },
                      fail: function(res) {
                        console.log('openSetting.failed' + res)
                      }
                    })
                  }
                  if (tip.cancel) {
                    wx.showToast({
                      title: '定位失敗',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            } else {
              wepy.showModal({
                title: '定位服務已關閉',
                content: '請在「設置」中打開定位服務,以便精準定位獲取更多優惠消息!'
              })
            }
          },
          fail: function (res) {
            console.log(res)
            wepy.showToast({
              title: '調用受權窗口失敗',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
      return result
    }
相關文章
相關標籤/搜索