百度地图引入到vue中

新建一个文件夹 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 = "https://api.map.baidu.com/api?v=2.0&ak="+ak+"&callback=init";
      script.onerror = reject;
      document.head.appendChild(script);
      console.log(ak)
    })
  }

引入:import {MP} from '../api/map.js'
在vue中初始化与调用

MP(_this.ak).then(BMap => {
						//在此调用api
					// 百度地图API功能
					_this.map = new BMap.Map("container"); // 创建Map实例
					_this.map.centerAndZoom(new BMap.Point(109.83, 19.03), 9); // 初始化地图,设置中心点坐标和地图级别
					var mapStyle ={
							features: ["road","building","water","land"],//隐藏地图上的"poi",
					 		style : 'midnight',
					 	};
					_this.map.setMapStyle(mapStyle);
					var pt = new BMap.Point(109.83, 19.03);
					var myIcon = new BMap.Icon("../../static/image/car.png", new BMap.Size(52, 26));
					var marker = new BMap.Marker(pt, {
					  icon: myIcon
				 	});  // 创建标注
					_this.map.addOverlay(marker);				
					 })

你可能感兴趣的:(百度地图引入到vue中)