欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

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

发表日期:2019-10 文章编辑:小灯 浏览次数:1350

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

<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
        })
      }
    })



本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://www.dengtar.com/23939.html
相关小程序
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.