小程序踩坑之地图定位不精准的问题

这篇博客介绍了如何在微信小程序中获取用户授权并使用腾讯位置服务获取经纬度,然后通过逆地理编码得到详细的地址信息。重点在于设置`coord_type`、`get_poi`及`poi_options`参数,以实现更精确的地址解析,适用于物流配送等场景。
摘要由CSDN通过智能技术生成

1、关于获取用户授权得到地理位置的代码:

 <view  bindtap="scanCode">
   点击按钮
 </view>
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js'); //1、必须引入
var qqmapsdk;
 
 
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    scanCodeMsg: "",
    address:'',
    latitude:'',
    longitude:'',
    
  },
   /**
   * 按钮绑定的方法
   */
  scanCode(){
    let _this = this;
    _this.getUserLocation();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({  //2、你申请的密钥
        key: '你申请的密钥'
    });
  },
  getUserLocation: function() {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        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({
                  success: function(dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权,调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
   // 微信获得经纬度
  getLocation: function() {
    let vm = this;
    wx.getLocation({  //不要冤枉他,他定位的其实还蛮准的
      type: 'wgs84',
      isHighAccuracy:true,
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed // 速度,以米/每秒计
        var accuracy = res.accuracy; // 位置精度
        vm.getLocal(latitude, longitude)
      },
      fail: function(res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function(latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      coord_type:1,
      get_poi:1,
      poi_options: 'policy=2;radius=600;page_size=20;page_index=1',  //3、这是重点!!!!
      success: function(res) {
        // let address = res.result.address
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        let formatted_addresses = res.result.formatted_addresses.recommend
        let address = province + city + formatted_addresses
        vm.setData({
          address:address,
          // province: province,
          // city: city,
          latitude: latitude,
          longitude: longitude
        })
 
      },
      fail:function(data){
        console.log(data);
      }
    });
  },
 
  
  
})

2、重点就在于这一段代码:

 coord_type:1, 
 get_poi:1,
 poi_options: 'policy=2;radius=600;page_size=20;page_index=1', //半径,取值范围 1-5000(米)policy=2 到家场景:筛选合适收货的poi,并会细化收货地址,精确到楼栋;

3、文档解析:微信小程序JavaScript SDK | 腾讯位置服务

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值