微信小程序位置授权并获取具体地理位置

一 获取用户的当前设置及二次授权

<view class="form-option" bindtap='getSetting'>
    <input placeholder="请选择收货地址" type="text" name="" bindinput="" value='{{address}}' />
</view>
//首次进入,点击‘选择地址’

图片描述

//弹出询问框

图片描述

拒绝授权后,再次进入该页面或者点击页面某按钮(获取位置)绑定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) {
              if (res.cancel) {
                //取消授权
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                //确定授权,通过wx.openSetting发起授权请求
                wx.openSetting({
                  success: function (res) {
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权,调用wx.getLocation的API
                      _this.goAddress();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //用户首次进入页面,调用wx.getLocation的API
          _this.goAddress();
        }
        else {
          // console.log('授权成功')
          //调用wx.getLocation的API
          _this.goAddress();
        }
      }
    })
  },

第二步:在需要获取地址的页面中:

//加载腾讯位置服务js文件(必须) 

申请密钥

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: '开发密钥(key)' // 必填
});

  wx.getLocation({
      type: "wgs84",
      success: function (res) {
        _this.setData({
          longitude: options.lat ? Number(options.lng) : res.longitude,
          latitude: options.lng ? Number(options.lat) : res.latitude,
          markers: [{
            latitude: options.lat ? Number(options.lat) : res.latitude,
            longitude: options.lng ? Number(options.lng) : res.longitude,
          }]
        })
        if (options.lat){
          _this.setData({
            address: options.address,
          })
        } else {
        //根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析,前面已引入SDK
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            success: function (addressRes) {
              const result = addressRes.result;
              _this.setData({
                address: result.address,
              })

            }
          })
        }
      },
      fail:function(){
        wx.navigateBack({
          delta:1
        })
      }
    })


  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值