码迷,mamicode.com
首页 > 微信 > 详细

小程序判断手机位置是否打开

时间:2019-11-14 15:15:28      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:小程序   gets   open   ons   oda   try   icon   ===   位置   

// 获取当前地地址
    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
    }

 

小程序判断手机位置是否打开

标签:小程序   gets   open   ons   oda   try   icon   ===   位置   

原文地址:https://www.cnblogs.com/fms-3/p/11857259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!