您好,欢迎来到汇意旅游网。
搜索
您的当前位置:首页分享在vue中使用百度地图的2种方法

分享在vue中使用百度地图的2种方法

来源:汇意旅游网

地图在项目中用得很多,最近也用了几次,好长时间不用都记不清了,闲暇时整理了vue里使用百度地图的2种方法,方便自己查看,也分享给大家,希望可以帮助有需要的人。

普遍的方法是:

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:'12873413029483740293'//密钥
 }
},
mounted(){
 this.$nextTick(function(){
 var _this = this;
 MP(_this.ak).then(BMap => {
 //在此调用api
 })
 }
}

Copyright © 2019- hids.cn 版权所有 赣ICP备2024042780号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务