时间:2021-07-01 10:21:17 帮助过:6人阅读
普遍的方法是:
1.index.html 中引入
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
2.新建个组件maps
注意 :不要把组件命名为map,因为html中有map标签,会报错
报错:Do not use built-in or reserved HTML elements as component id:map
3.然后就可以直接再组件了写相关代码了
mounted(){
var map = new BMap.Map('map')
var point = new BMap.Point(108.840053, 34.129522)
map.centerAndZoom(point, 14)
//...
}另一个方法:
1.新建一个map.js
export function MP(ak) {
return new Promise(function (resolve, reject) {
window.onload = function () {
resolve(BMap)
}
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://api.map.baidu.com/api?v=2.0&ak="+ak+"&callback=init";
script.onerror = reject;
document.head.appendChild(script);
})
}2.在需要用到的地图的vue页面中引入
import {MP} from './map.js'3.在vue页面中调用
data:{
return{
ak:'1287348913029483740293'//密钥
}
},
mounted(){
this.$nextTick(function(){
var _this = this;
MP(_this.ak).then(BMap => {
//在此调用api
})
}
}以上就是分享在vue中使用百度地图的2种方法的详细内容,更多请关注Gxl网其它相关文章!