Vue2引入百度地图使用

  1. npm install vue-baidu-map --save

  2. 在main.js文件中设置你申请的百度的密钥key

    //main.js文件
    import BaiduMap from 'vue-baidu-map'
    Vue.use(BaiduMap, {
      ak: '你的秘钥'
    })	
    
  3. 组件中直接使用,全部代码如下

    <template>
      <div class="baidu-css">
        <baidu-map
          class="bm-view"
          :center="center"
          :scroll-wheel-zoom="true"
          :zoom="zoom"
          @ready="handler"
        >
          <bm-geolocation
            anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
            :showAddressBar="true"
            :autoLocation="true"
          ></bm-geolocation>
        </baidu-map>
      </div>
    </template>
    
    <script>
    export default {
      name: "new",
      components: {},
      data() {
        return {
          center: { lng: 0, lat: 0 }, //定位
          zoom: 3, //缩放等级
        };
      },
      methods: {
        handler({ BMap, map }) {
          console.log(BMap, map);
          this.center.lng = 114.299815;
          this.center.lat = 30.595174;
          // this.zoom = 10 //市级区域
          this.zoom = 13; //更精确的缩放
        },
      },
      mounted() {},
    };
    </script>
    
    <style lang="scss" scoped>
    .bm-view {
      width: 100%;
      height: 800px;
    }
    .baidu-css {
      margin-left: 20px;
      margin-top: 20px;
      margin-bottom: 20px;
    }
    </style>
    
    
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值