微信小程序授权获取地理位置wx.getLocation

1.效果

2.需要依赖包

        qqmap-wx-jssdk.min.js

        下载地址:微信小程序-获取地理位置-Node.js文档类资源-CSDN下载

3.代码 

  var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');

  getSetting: function () { // 获取用户地理位置授权
    const _this = this;
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          //未授权
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置,请确认授权',
            success: function (res) {
              console.log(res)
              if (res.cancel) {
                //取消授权
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
                _this.setData({
                  region: ['广东省', '广州市', '南沙区'],
                  [`enrollForm.contactProvince`]: '广东省',
                  [`enrollForm.contactCity`]: '广州市',
                  [`enrollForm.contactRegion`]: '南沙区',
                  isGetAddSetting: false
                })
              } else if (res.confirm) {
                //确定授权,通过wx.openSetting发起授权请求
                console.log('获取权限')
                wx.openSetting({
                  success: function (res) {
                    console.log(res)
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      console.log('授权成功')
                      //再次授权,调用wx.getLocation的API
                      _this.getAddress();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //用户首次进入页面,调用wx.getLocation的API
          _this.getAddress();
        } else {
          // console.log('授权成功')
          //调用wx.getLocation的API
          _this.getAddress();
        }
      }
    })
  },


  getAddress() { // 获取用户地理位置
    console.log('获取地址')
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        var qqmapsdk = new QQMapWX({
          key: 'MPCBZ-WF5H6-54VSZ-ENDBN-5IYK2-YTBBV' // 必填
        });
        qqmapsdk.reverseGeocoder({
          success: function (addressRes) {
            const result = addressRes.result;
            console.log(result)
            that.setData({
              region: [result.address_component.province, result.address_component.city, result.address_component.district],
              [`enrollForm.contactProvince`]: result.address_component.province,
              [`enrollForm.contactCity`]: result.address_component.city,
              [`enrollForm.contactRegion`]: result.address_component.district,
              [`enrollForm.contactAddress`]: result.address_component.street_number,
              isGetAddSetting: false
            })
          }
        })
      }
    })
  },

wx.getLocation的调用返回打印

qqmapsdk.reverseGeocoder的调用返回打印

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信小程序中,使用wx.getLocation可以获取用户的地理位置信息。但是,对于wx.getLocation的重复调用需要注意一些问题。 首先,频繁调用wx.getLocation可能导致用户体验下降。因为每次调用wx.getLocation都需要用户授权,并且在一些设备上获取地理位置可能需要较长的时间。因此,如果在短时间内多次调用wx.getLocation,用户可能会感到繁琐和不满。 其次,在代码中重复调用wx.getLocation可能会带来一些逻辑上的混乱。例如,在一个页面中,如果重复调用wx.getLocation获取用户位置,可能会导致获取到的位置信息被覆盖或者混乱,而无法正确处理。 为了避免重复调用wx.getLocation带来的问题,可以采取以下的措施: 1. 在获取到位置信息后,将其存储在一个合适的变量中,以便之后的使用。这样可以避免重复调用wx.getLocation而导致的延迟和用户体验问题。 2. 通过适当的条件判断,避免重复调用wx.getLocation。例如,可以在用户第一次访问页面时调用wx.getLocation获取位置信息,并在之后的访问中直接使用之前获取的位置信息。 3. 如果需要在某些特定的情况下更新位置信息,可以通过用户交互来触发wx.getLocation的调用,而不是在每次页面加载时都去获取。 总之,对于wx.getLocation的重复调用,开发者需要综合考虑用户体验和业务逻辑的需求,避免频繁调用wx.getLocation,同时合理利用已获取的位置信息,以提升小程序的性能和用户满意度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值