由于BMapGL没有海量打点的方法、Bmap3.0没有卫星地图,在各种取舍下还是使用了BMapGL进行打点 并且在开发过程中发现百度地图的画线方法Polyline实时的画线是监听不到点击事件的(必须要一条完整的线)所有只能用Marker进行打点
通过websocket实时发送数据
data: {
latitude: 36.00
longitude: 116.11
sendTime: 1650006526000
Id: 1
}
type: 2
vue
<template>
<div class="viewBox">
<div class="bm-view" id="container"></div>
</div>
</template>
<script>
import { sendWebsocket, closeWebsocket } from "@/utils/websocket.js";
import { reactive,toRefs,onBeforeMount,onMounted} from 'vue'
import { initMap, setPoint,setMarker} from "../utils/BDmap";
import { ElLoading } from "element-plus";
export default {
name: 'BDmap',
setup() {
onMounted(() => {
openWebsocket(param);
Loading();
initMap((tilesloaded) => {
loadingState.close();
});
})
function Loading() {
loadingState = ElLoading.service({
lock: true,
text: "正在加载地图",
background: "rgba(0, 0, 0, 0.7)",
});
}
function wsMessage(data) {
if (data.type === 2) {
setMarker([data.data]);
}
}
function openWebsocket(param) {
sendWebsocket(param, wsMessage, wsError);
}
return {
}
}
};
</script>
BDmap.js
let map
let init = [116.404, 39.925];
let points = [];
let Marker
let opts = {
width: 200,
height: 80,
title: '信息'
};
export function initMap(callback) {
map = new BMapGL.Map("container");
setPoint(init);
map.addControl(new BMapGL.ScaleControl());
map.enableScrollWheelZoom(true);
map.setMapType(BMAP_EARTH_MAP);
map.addEventListener("tilesloaded", function () {
callback('tilesloaded')
});
}
export function setPoint(point) {
map.centerAndZoom(new BMapGL.Point(point[0], point[1]), 16);
}
function removeMaker() {
let allOverlay = map.getOverlays();
map.removeOverlay(allOverlay[0]);
}
export function setMarker(mapData) {
let allOverlay = map.getOverlays();
if (allOverlay.length >= 400) {
removeMaker()
}
let point = new BMapGL.Point(mapData.longitude, mapData.latitude);
Marker = new BMapGL.Marker(point);
Marker.id= mapData.id;
Marker.sendTime = mapData.sendTime;
Marker.addEventListener('click', function (e) {
let infoWindow = new BMapGL.InfoWindow(`
<p>id:${e.currentTarget.id}</p>
<p>发送时间:${parseTime(e.currentTarget.sendTime)}</p>`, opts);
map.openInfoWindow(infoWindow, point);
});
map.addOverlay(Marker);
}
//例
以上
|