Vue2.0與 [百度地圖] 結合使用:
1.vue init webpack-simple vue-baidu-map
2.下載axios
cnpm install axios;
3.在main.js中引入axios,並使用
import axios from 'axios'
/* 把axios對象掛到Vue實例上面,其他組件在使用axios的時候直接 this.$http就可以了 */
Vue.prototype.$http = axios;
4.引入百度地圖的js秘鑰--->最好在index.js中直接引入
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰">
5.新建文件Map.vue,作為map組件使用
<template>
<div id="div1" :style="style"></div>
</template>
<script>
export default{
data:(){
return{
style:{
width:'100%',
height:this.height+'px'
}
}
},
props:{ //里面存放的也是數據,與data里面的數據不同的是,這里的數據是從其他地方得到的數據
height:{
type:Number,
default:300
},
longitude:{}, //定義經度
latitude:{} //定義緯度
},
mounted(){
var map = new BMap.Map("div1");
var point = new BMap.Point(this.longitude,this.latitude);
map.centerAndZoom(point, 12);
var marker = new BMap.Marker(point);// 創建標注
map.addOverlay(marker);
}
}
</script>
6.假如最終組件在App.vue里面使用
<template>
<!-- 綁定latitude屬性,傳給Map.vue,這樣通過props就拿到了數據,類似於父組件往子組件傳數據 -->
<MapView :height="height" :longitude="longitude" :latitude="latitude"></MapView>
</template>
import MapView from './components/Map.vue'
export default{
data(){
return{
height:300,
longitude:116.404,
latitude:39.915
}
},
componets:{
MapView
},
mounted(){
}
}
項目連接:https://github.com/yufann/vue-baidu-map
這個項目中有兩個百度地圖:
第一個地圖實現效果是:vue+webpack+百度地圖-->實現父子組件的通信(跳躍的圖標(沒有使用axios))
第二個地圖實現效果是:vue+webpack+axios+百度地圖-->實現父子組件的通信(給多個點添加信息窗口(使用了axios))
