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

      最近做了一个小程序,需要获取用户的地理位置,用户授权后将地理位置逆解析后带到下一个页面上进行展示。但逆解析出来的结果与实际位置大概差了600m左右,一开始查了很多资料,都以为是经纬度获取就不精准,导致了后面的逆解析出现了问题,真的是抠破头的想解决办法,最后疲惫的时候无心扫过地图文档才找到了解决办法,分享出来给大家,愿2021大家头发健在~~~

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、文档解析:https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodReverseGeocoder

4、根据文档上的参数自己设置了一下,然后定位显示的位置就很精准了,希望这篇文章对你有用哈~~ 


版权声明:本文为qq_42181069原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。