Vue利用百度地图初始化页面定位

5 篇文章 0 订阅

Vue利用百度地图初始化页面定位

结果展示

获取百度ak

引入控件

  • npm install vue-baidu-map --save

main.js中引入

import Vue from 'vue';
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
    ak: '申请的百度地图秘钥'
})

页面控件

<div>
  <baidu-map :center="center" :zoom="zoom" :scroll-wheel-zoom="true" style="height: 65vh" @ready="handler"
             @click="getClickInfo">
    <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
    <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
    <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
  </baidu-map>
</div>

js部分

<script>
  export default {
    data() {
      return {
        center: {lng: 0, lat: 0}, //经纬度
        zoom: 13 //地图展示级别
      };
    },
    methods: {
      handler({BMap, map}) {
        this.zoom = this.zoom;
        let self = this;

        //关键词定位
        map.centerAndZoom("太原", 11);

        //浏览器位置定位
        // var geolocation = new BMap.Geolocation();
        // geolocation.getCurrentPosition(function (r) {
        //   let pi = 3.141592653589793 * 3000.0 / 180.0;
        //   let x = r.longitude;
        //   let y = r.latitude;
        //   let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
        //   let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
        //
        //   self.center.lng = z * Math.cos(theta) + 0.0065;
        //   self.center.lat = z * Math.sin(theta) + 0.006;
        // }, {enableHighAccuracy: true})

      },
      getClickInfo(e) {
        this.center.lng = e.point.lng;
        this.center.lat = e.point.lat;
      },
    }
  }
</script>

注意事项

  1. 页面地图控件必须指定高度
  2. 浏览器定位有误差
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值