使用vue框架的H5项目通过(百度地图和腾讯地图)自动获取经纬度的解决方法

亲测https协议才能精准定位(位置更精准),http协议的项目存在获取失败及经纬度偏差大的问题

注意 :小距离偏差可能是坐标系导致的

  • 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
  • 百度地图使用的是BD-09坐标系
  • 底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系
    不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。

坐标转换工具地址https://tool.lu/coordinate/
经纬度查询工具地址http://jingweidu.757dy.com/

百度地图

申请ak地址https://lbsyun.baidu.com/apiconsole/key#/home
参考百度文档地址https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/geoloaction

本示例使用百度文档中的浏览器定位

1、引用百度地图API文件

<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&type=webgl&ak=your ak"></script> //替换自己的ak

2、页面调用

mounted() {
    this.lib_getPosition(); //百度
  },
methods: {
    lib_getPosition() {
      const BMap = window.BMapGL;
      const BMapGeolocation = new BMap.Geolocation();
      BMapGeolocation.getCurrentPosition(r => {
        if (r.latitude && r.longitude) {
          console.log("baidu", r);
          alert(`百度成功-${JSON.stringify(r)}`);
          // 获取到经纬度(百度转换高德经纬度)
        } else {
          console.log(22);
        }
      });
    },
}

腾讯地图

申请key地址https://lbs.qq.com/dev/console/application/mine
参考腾讯文档地址https://lbs.qq.com/webApi/component/componentGuide/componentGeolocation

本示例使用腾讯文档中的调用方式一

1、引用腾讯地图API文件

<script type="text/javascript" src="https://mapapi.qq.com/web/mapComponents/geoLocation/v/geolocation.min.js"></script>

2、页面调用

mounted() {
    this.getUserLocation(); //腾讯
  },
methods: {
	getUserLocation() {
      console.log(88);
      let geolocation = new qq.maps.Geolocation(
        "your key",//替换自己的key
        "myAPP"
      );
      geolocation.getLocation(this.showPosition, this.errorPosition); //开启定位
    },
    //成功返回的信息,挑取自己所需要的
    showPosition(position) {
      console.log(11111);
      this.lat = position.lat;
      this.lng = position.lng;
      var location = position.lat + "," + position.lng;
      console.log(location, "location");
      alert(`腾讯成功1-${JSON.stringify(position)}`);
    },
    // 定位失败 继续尝试定位
    errorPosition(e) {
      console.log(e, "定位失败,再次进行定位");
      alert(`腾讯失败1-${JSON.stringify(e)}`);
    },
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue使用百度地图API自动获取地址和经纬度,你需要完成以下步骤: 1. 在百度地图开放平台上注册账号并创建应用,获取API密钥。 2. 在Vue项目中引入百度地图API的JS文件,在index.html中添加以下代码: ``` <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=your_api_key"></script> ``` 将 `your_api_key` 替换为你在百度地图开放平台上创建应用后获取的API密钥。 3. 在Vue组件中使用以下代码,创建地图实例: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; }, ``` 其中,`map-container` 是放置地图的DOM元素的ID。 4. 在Vue组件中使用以下代码,获取用户当前位置的经纬度: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; } else { alert('定位失败'); } }); }, ``` 其中,`geolocation.getCurrentPosition` 方法用于获取用户当前位置的经纬度。如果获取成功,则将经纬度赋值给 `latitude` 和 `longitude` 变量。如果获取失败,则弹出提示框。 5. 在Vue组件中使用以下代码,将经纬度转换成具体的地址: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; var point = new BMap.Point(this.longitude, this.latitude); var geoc = new BMap.Geocoder(); geoc.getLocation(point, (rs) => { this.address = rs.address; }); } else { alert('定位失败'); } }); }, ``` 其中,`BMap.Point` 方法用于创建一个地理位置点的实例,需要传入经度和纬度。`BMap.Geocoder` 方法用于将经纬度转换成具体的地址。`geoc.getLocation` 方法用于获取地址信息,并将地址信息赋值给 `address` 变量。 这样,你就可以在Vue使用百度地图API自动获取地址和经纬度了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值