vue中引入百度地图
一、通过vue注册的方式引入
安装百度地图
$ npm install vue-baidu-map --save
引入
使用百度地图首先需要去百度地图开放平台申请ak密钥,登录百度账号后:控制台->应用管理->我的应用->创建应用即可,创建完成后即可看到生成的ak密钥
1)全局注册,在工程的main.js中引入
import Vue from "vue";
import baiduMap from "vue-baidu-map";
Vue.use(baiduMap, { ak: mapAK });
2)局部注册,在使用地图的页面引入 地图组件存放在vue-baidu-map/components 文件夹下,可根据需要按模块引用
import baiduMap from "vue-baidu-map/components/map/Map.vue";
import bmMarker from "vue-baidu-map/components/overlays/Marker.vue";
import bmInfoWindow from "vue-baidu-map/components/overlays/InfoWindow.vue";
export default {
components: {
baiduMap,
bmMarker,
bmInfoWindow
}
}
使用
更多详细使用参考官网API文档:javaScript API,web服务API
<template>
<baidu-map class="bm-view" ak="mapAK">
</baidu-map>
</template>
二、通过js引入
1)引入map
① 基础map 定义一个loadMap.js
export default function loadBMap(ak) {
return new Promise((resolve, reject) => {
if (typeof window.BMap !== "undefined") {
resolve(window.BMap);
return true;
}
window.onBMapCallback = function () {
resolve(window.BMap);
return true;
};
const script = document.createElement("script");
script.type = "text/javascript";
script.src =
`https://api.map.baidu.com/api?v=3.0&ak=${ak}&callback=onBMapCallback&s=1`;
script.onerror = reject;
document.head.appendChild(script);
return true;
});
}
② BmapGL(基于百度地图JavaScript GL版API封装的React组件库,有需要可引入) 定义一个bmpgl.js
export default function BMPGL(ak) {
return new Promise((resolve, reject) => {
window.init = function () {
resolve(BMapGL)
};
const script = document.createElement("script");
script.type = "text/javascript";
script.src = `https://api.map.baidu.com/api?v=1.0&type=webgl&ak=${ak}&callback=init`;
script.onerror = reject;
document.head.appendChild(script);
});
}
引入
import loadBMap from "../loadMap";
import BMPGL from "../bmpgl";
mounted() {
const p = loadBMap(this.mapAK);
p.then(() => {
this.initMap();
});
}
methods: {
initMap() {
const that = this;
this.map = new window.BMap.Map("map-container", {
enableMapClick: false
});
const point = new window.BMap.Point(
this.longitude,
this.latitude
);
this.map.centerAndZoom(point, 15);
this.mk = new window.BMap.Marker(point, { enableDragging: true });
this.map.addOverlay(this.mk);
this.mk.addEventListener("dragend", (e) => {
that.getAddrByPoint(e.point, true);
});
const navigationControl = new window.BMap.NavigationControl({
anchor: window.BMAP_ANCHOR_TOP_RIGHT,
type: window.BMAP_NAVIGATION_CONTROL_LARGE
});
this.map.addControl(navigationControl);
if (this.unloadLocation) {
this.geolocation();
}
},
getAddrByPoint(point, movePoint = false) {
if (this.unloadLocation || movePoint) {
this.unloadLocation = false;
this.getGeocoding(point);
this.longitude = point.lng;
this.latitude = point.lat;
this.mk.setPosition(point);
this.map.panTo(point);
}
},
geolocation() {
BMPGL(this.mapAk).then((BMapGL) => {
const geolocation = new BMapGL.Geolocation();
geolocation.getCurrentPosition(function(r) {
if (this.getStatus() === window.BMAP_STATUS_SUCCESS) {
this.longitude = r.lng;
this.latitude = r.lat;
} else {
alert(`failed${this.getStatus()}`);
}
});
});
},
}
|