vue項目中接入百度地圖api

vue項目中接入百度地圖api

1、申請百度地圖祕鑰

申請地址: http://lbsyun.baidu.com/apiconsole/key

2、安裝插件

$ npm install vue-baidu-map --save

3、 main.js中引用vue-baidu-map組件

import Vue from 'vue';
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
    ak: '你申請的百度地圖祕鑰' //官方提供的ak祕鑰
})
// 這是全局註冊的方法, 也可採用局部註冊,只在使用該組件的頁面調用組件名即可,具體參考官方文檔

Vue Baidu Map 官方文檔地址.

4、註冊組件map.vue

<template>
  <div class="mapbox">
    <baidu-map :center="center" :zoom="zoom" :scroll-wheel-zoom="true" style="height:100vh" @ready="handler" @click="getClickInfo" >
      <!-- 必須給容器指高度,不然地圖將顯示在一個高度爲0的容器中,看不到 -->
      <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>
    <!-- 因爲我採用的是全局註冊,所以不用再在該頁面上註冊components -->
  </div>
</template>
<script>
export default {
  name: "mapbox",
  data() {
    return {
      center: { lng: 0, lat: 0 }, //經緯度
      zoom: 13 //地圖展示級別
    };
  },
  methods: {
    handler({ BMap, map }) {
      console.log(BMap, map);
      this.center.lng = 113.6313915479;
      this.center.lat = 34.7533581487;
      this.zoom = this.zoom;
    },
    getClickInfo(e) {
      console.log(e.point.lng);
      console.log(e.point.lat);
      this.center.lng = e.point.lng;
      this.center.lat = e.point.lat;
    }
  }
};
</script>

注:該博客爲項目過程記錄,參考了他人的博客,非原創

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章