BigScreenUI/src/components/mapBaidu/mapLine.vue

174 lines
4.9 KiB
Vue
Raw Normal View History

2023-11-30 18:37:22 +08:00
<template>
2023-12-04 10:22:26 +08:00
<div v-if="domShow" :id="'equipmentIdMap' + domId" class="map">
2023-11-30 18:37:22 +08:00
</div>
</template>
<script setup lang="ts">
2023-12-04 12:37:03 +08:00
2023-11-30 18:37:22 +08:00
let map: any = null
2023-12-04 10:22:26 +08:00
let centerPoint: any = null
2024-03-20 11:16:34 +08:00
let domId = ref(0)
2023-12-04 10:22:26 +08:00
let domShow = ref(false)
2023-12-04 11:13:01 +08:00
let animiationLineList = []
2024-03-20 11:16:34 +08:00
const state = reactive({
2023-12-04 12:37:03 +08:00
lineStyle: {
2024-03-20 11:16:34 +08:00
strokeColor: "#0e83ed",
strokeWeight: 3,
strokeOpacity: 0.6,
2023-12-04 12:37:03 +08:00
}
})
2023-11-30 18:37:22 +08:00
onMounted(() => {
console.log("map", "map")
})
2023-12-04 12:37:03 +08:00
let linePointList: any = []
2023-12-04 10:22:26 +08:00
2023-11-30 18:37:22 +08:00
const initMap = () => {
2023-12-04 11:13:01 +08:00
map = new BMapGL.Map("equipmentIdMap" + domId.value);
2024-03-19 18:07:19 +08:00
map.centerAndZoom(new BMapGL.Point(centerPoint), 18);
2023-11-30 18:37:22 +08:00
console.log("map", map)
map.enableScrollWheelZoom();
setTimeout(() => {
2023-12-04 10:22:26 +08:00
// initCustomDot()
const startIcon = {
imgUrl: '/src/assets/img/mapStart.png',
2024-03-19 18:07:19 +08:00
position: [linePointList[0].lng, linePointList[0].lat],
2023-12-04 10:22:26 +08:00
size: [60, 60]
2023-11-30 18:37:22 +08:00
}
2023-12-04 10:22:26 +08:00
const endIcon = {
2024-03-18 15:24:40 +08:00
imgUrl: '/src/assets/img/mapEnd.png',
2024-03-19 18:07:19 +08:00
position: [linePointList[linePointList.length - 1].lng, linePointList[linePointList.length - 1].lat],
2023-12-04 10:22:26 +08:00
size: [60, 60]
2023-11-30 18:37:22 +08:00
2023-12-04 10:22:26 +08:00
}
initIcon(startIcon.imgUrl, startIcon.position, startIcon.size)
initIcon(endIcon.imgUrl, endIcon.position, endIcon.size)
2023-12-04 11:13:01 +08:00
// initPolyline()
initPolylineAnimation()
2023-12-03 19:03:32 +08:00
setViewPortFn()
2023-12-04 10:22:26 +08:00
}, 200)
2023-11-30 18:37:22 +08:00
}
2023-12-04 10:22:26 +08:00
const initCustomDot = (centerPoint: any) => {
2024-03-19 18:07:19 +08:00
function ComplexCustomOverlay(centerPoint: any) {
2023-11-30 18:37:22 +08:00
}
2023-12-04 11:13:01 +08:00
ComplexCustomOverlay.prototype = new BMapGL.Overlay();
2024-03-19 18:07:19 +08:00
ComplexCustomOverlay.prototype.initialize = function (map: any) {
2023-11-30 18:37:22 +08:00
const div = this._div = document.createElement("div");
div.style.position = "absolute";
div.style.width = "30px";
div.style.height = "30px";
div.style.background = "#fff";
div.style.borderRadius = "50%";
const divInner = this._divInner = document.createElement("div");
divInner.style.position = "absolute";
divInner.style.width = "20px";
divInner.style.height = "20px";
divInner.style.left = "5px";
divInner.style.top = "5px";
divInner.style.background = "#0788e6";
divInner.style.borderRadius = "50%";
map.getPanes().labelPane.appendChild(div);
map.getPanes().labelPane.appendChild(divInner);
return div;
}
ComplexCustomOverlay.prototype.draw = function () {
2023-12-04 10:22:26 +08:00
const pixel = map.pointToOverlayPixel(centerPoint);
2023-11-30 18:37:22 +08:00
this._div.style.left = pixel.x + "px";
this._div.style.top = pixel.y + "px";
this._divInner.style.left = pixel.x + 5 + "px";
this._divInner.style.top = pixel.y + 5 + "px";
}
2023-12-04 10:22:26 +08:00
const myCompOverlay: any = new ComplexCustomOverlay(centerPoint);
2023-11-30 18:37:22 +08:00
map.addOverlay(myCompOverlay);
}
2023-12-04 11:13:01 +08:00
const initPolylineAnimation = () => {
2024-03-20 11:16:34 +08:00
var pl = new BMapGL.Polyline(linePointList, state.lineStyle);
2023-12-04 11:13:01 +08:00
// setTimeout('start()', 3000);
const trackAni = new BMapGLLib.TrackAnimation(map, pl, {
overallView: true,
tilt: 30,
2024-03-20 11:16:34 +08:00
duration: 4000,
2023-12-04 11:13:01 +08:00
delay: 300,
});
start()
function start() {
trackAni.start();
}
}
2023-12-04 10:22:26 +08:00
const initIcon = (imgUrl: any, position: any, size: any) => {
2024-03-19 18:07:19 +08:00
// var myIcon = new BMapGL.Icon("https://api.map.baidu.com/img/markers.png", new BMapGL.Size(60, 60));
var myIcon = new BMapGL.Icon(imgUrl, new BMapGL.Size(60, 60));
2023-11-30 18:37:22 +08:00
// 创建Marker标注使用小车图标
2023-12-04 11:13:01 +08:00
var pt = new BMapGL.Point(...position);
var marker = new BMapGL.Marker(pt, {
2023-11-30 18:37:22 +08:00
icon: myIcon
});
// 将标注添加到地图
map.addOverlay(marker);
}
2023-12-04 10:22:26 +08:00
const setViewPortFn = () => {
const boundList = linePointList
2023-12-03 19:03:32 +08:00
map.setViewport(boundList)
}
2023-12-04 10:22:26 +08:00
// var pointArr = [new BMap.Point(106.607421,29.570857),new BMap.Point(106.620931,29.586185)];
const getcenterpoiont = (pointStart: any, pointEnd: any) => {
const lng1 = Number(pointStart.lon);
const lat1 = Number(pointStart.lat);
const lng2 = Number(pointEnd.lon);
const lat2 = Number(pointEnd.lat);
const lngca = (Math.max(lng1, lng2) - Math.min(lng1, lng2)) / 2;
const latca = (Math.max(lat1, lat2) - Math.min(lat1, lat2)) / 2;
const lngcenter = Math.min(lng1, lng2) + lngca;
const latcenter = Math.min(lat1, lat2) + latca;
2023-12-04 11:13:01 +08:00
const pointcenter = new BMapGL.Point(lngcenter, latcenter);
2023-12-04 10:22:26 +08:00
return pointcenter;
}
2023-12-04 12:37:03 +08:00
const initMapLine = (res: any, id: any) => {
domId.value = id
2023-12-04 10:22:26 +08:00
domShow.value = false
2023-12-04 12:37:03 +08:00
res.forEach((ele: any) => {
linePointList.push(new BMapGL.Point(Number(ele.lon), Number(ele.lat)))
})
nextTick(() => {
2023-12-04 10:22:26 +08:00
domShow.value = true
2023-12-04 12:37:03 +08:00
setTimeout(() => {
2023-12-04 10:22:26 +08:00
initMap()
centerPoint = getcenterpoiont(res[0], res[res.length - 1])
console.log("centerPoint", centerPoint)
initCustomDot(centerPoint)
})
2023-12-04 12:37:03 +08:00
})
2023-12-03 19:03:32 +08:00
}
2023-12-04 12:37:03 +08:00
2023-12-03 19:03:32 +08:00
defineExpose({
initMapLine
})
2023-11-30 18:37:22 +08:00
</script>
<style scoped lang="scss">
.map {
width: 100%;
height: 100%;
}
</style>