小程序定位授权

/**判断是否授权定位 */

  getSetting() {

    let that = this;

    // 获取当前城市

    wx.getSetting({

      success: (res) => {

        console.log(res)

        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权

          that.setData({

            showMain: true,

          })

          wx.showModal({

            content: `${app.config.referer}获取你的位置信息`,

            success: function (res) {

              if (res.cancel) {

                wx.showToast({

                  title: '授权失败',

                  icon: 'success',

                  duration: 1000

                })

                wx.reLaunch({

                  url: `/pages_A/vending_20220701/apply/apply?type=4`

                })

              } else if (res.confirm) {

                wx.openSetting({

                  success: function (dataAu) {

                    if (dataAu.authSetting["scope.userLocation"] == true) {

                      wx.showToast({

                        title: '授权成功',

                        icon: 'success',

                        duration: 1000

                      })

                      //再次授权,调用getLocationt的API

                      that.getLocation();

                    } else {

                      wx.showToast({

                        title: '授权失败',

                        icon: 'success',

                        duration: 1000

                      })

                      wx.reLaunch({

                        url: `/pages_A/vending_20220701/apply/apply?type=4`

                      })

                    }

                  }

                })

              }

            }

          })

        } else if (res.authSetting['scope.userLocation'] == undefined) { //初始化进入

          that.getLocation();

          that.setData({

            showMain: true,

          })

        } else { //授权后默认加载

          that.getLocation();

        }

      }

    })

  },

  /**获取定位数据 */

  getLocation() {

    let that = this;

    wx.getLocation({

      type: 'wgs84',

      success(res) {

        const latitude = res.latitude

        const longitude = res.longitude

        console.log(res);

        that.setData({

          latitude,

          longitude,

        })

        console.log(111)        

        that.getDistance(latitude, longitude);

      },

      fail() {

        console.log(7777)       

        wx.reLaunch({

          url: `/pages_A/vending_20220701/apply/apply?type=4`

        })

      }

    })

  },

进入页面,首先判断小程序的授权情况,根据不同情况做不同的操作,之后走getLocation()方法。wx.getLocation可直接调起按钮让用户同意授权,可直接获取经纬度,也可将经纬度放入接口调用,查询该定位所在省市区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值