<template>
|
<div id="taskMap" />
|
</template>
|
<script setup>
|
import * as Cesium from 'cesium'
|
import { Cartesian3, Math as CesiumMath, Terrain, Viewer } from 'cesium'
|
import AmapMercatorTilingScheme from '@/utils/cesium/AmapMercatorTilingScheme'
|
import { analyzeKmzFile, removeTextKey, XMLToJSON } from '@/utils/cesium/kmz'
|
import ImageTrailMaterial from '@/utils/cesium/ImageTrailMaterial'
|
import lineImg from '@/assets/images/arrow-right-blue.png';
|
import Startingpointicon from '@/assets/images/Startingpointicon.png';
|
import EndPointicon from '@/assets/images/EndPointicon.png';
|
import uavImg from '@/assets/images/home/useUavHome/uavImg.png'
|
import { getCenterPoint } from '@/utils/cesium/mapUtil'
|
|
const props = defineProps(['wayLineFile'])
|
|
|
const imageryProvider_ammapSL = new Cesium.UrlTemplateImageryProvider({
|
url: 'https://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
|
layer: 'tdtVecBasicLayer',
|
style: 'default',
|
format: 'image/png',
|
tileMatrixSetID: 'GoogleMapsCompatible',
|
subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
|
maximumLevel: 18,
|
tilingScheme: new AmapMercatorTilingScheme(),
|
credit: 'amap_SL',
|
})
|
let viewer = null
|
const init = () => {
|
viewer = new Viewer('taskMap', {
|
terrain: Terrain.fromWorldTerrain(),
|
infoBox: false, // 禁用沙箱,解决控制台报错
|
animation: false, // 左下角的动画仪表盘
|
baseLayerPicker: false, // 右上角的图层选择按钮
|
geocoder: false, // 搜索框
|
homeButton: false, // home按钮
|
sceneModePicker: false, // 模式切换按钮
|
timeline: false, // 底部的时间轴
|
navigationHelpButton: false, // 右上角的帮助按钮,
|
selectionIndicator: false, // 是否显示选择指示器
|
baseLayer: false,
|
fullscreenButton: false,
|
// sceneMode: Cesium.SceneMode.COLUMBUS_VIEW,
|
})
|
viewer.imageryLayers.addImageryProvider(imageryProvider_ammapSL)
|
viewer.scene.morphTo2D(0);
|
}
|
|
// 渲染线和点
|
const renderingLine = lineObj => {
|
const positions = lineObj.Placemark.map(item => {
|
const [lon, lat] = item.Point.coordinates.split(',')
|
return Cartesian3.fromDegrees(Number(lon), Number(lat))
|
})
|
// viewer.entities.add({
|
// position: positions[0],
|
// billboard: {
|
// image: new Cesium.ConstantProperty(uavImg),
|
// width: 24,
|
// height: 24,
|
// },
|
// });
|
viewer.entities.add({
|
polyline: {
|
width: 4,
|
positions: positions,
|
material: new ImageTrailMaterial({
|
color: { alpha: 1, blue: 1, green: 1, red: 1 },
|
speed: 20,
|
image: lineImg,
|
repeat: { x: Math.floor(40), y: 1 },
|
}),
|
clampToGround: false,
|
},
|
})
|
console.log('positions',positions)
|
positions.forEach((point, index) => {
|
let setting = {};
|
if (index === 0) {
|
//TODO
|
} else if (index === 1) {
|
setting = {
|
position: point,
|
id: `point_${index}`,
|
billboard: {
|
image: Startingpointicon,
|
outlineWidth: 0,
|
width: 20,
|
height: 20,
|
scale: 1.0,
|
},
|
}
|
} else if (index === positions.length - 1) {
|
setting = {
|
position: point,
|
id: `point_${index}`,
|
billboard: {
|
image: EndPointicon,
|
outlineWidth: 0,
|
width: 20,
|
height: 20,
|
scale: 1.0,
|
},
|
}
|
} else {
|
setting = {
|
position: point,
|
id: `point_${index}`,
|
label: {
|
text: `${index}`,
|
font: 'bold 14px serif',
|
style: Cesium.LabelStyle.FILL,
|
verticalOrigin: Cesium.VerticalOrigin.CENTER, // 垂直居中
|
horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平居中
|
pixelOffset: new Cesium.Cartesian2(0, 0), // 根据需要调整偏移量
|
eyeOffset: new Cesium.Cartesian3(0, 0, -10) // 使标签在点的上方
|
},
|
|
point: {
|
pixelSize: 24,
|
color: new Cesium.Color.fromBytes(255, 186, 0, 255),
|
},
|
|
offset: new Cesium.Cartesian2(10, 30),
|
}
|
}
|
viewer.entities.add(setting)
|
});
|
}
|
|
// 飞到中心点
|
function flyToPoints(lngLatArr) {
|
if (!Array.isArray(lngLatArr) || lngLatArr.length === 0) return
|
const positions = lngLatArr.map(([lon, lat]) =>
|
Cesium.Cartesian3.fromDegrees(Number(lon), Number(lat))
|
)
|
// 计算包围盒 BoundingSphere(所有点的外接球)
|
const boundingSphere = Cesium.BoundingSphere.fromPoints(positions)
|
viewer.camera.flyToBoundingSphere(boundingSphere, {
|
duration: 0,
|
offset: new Cesium.HeadingPitchRange(0, 0, boundingSphere.radius * 2),
|
})
|
}
|
|
// const drawLine = () => {
|
// console.log('尽量么');
|
// // props.wayLineFile.way_lines.forEach(item => {
|
// // let prexUrl = ref(import.meta.env.VITE_APP_AIRLINE_URL+ props.wayLineFile);
|
// let prexUrl2 = ref('https://wrj.shuixiongit.com/minio/cloud-bucket/wayline/20241224/wayline_1735032173222.kmz');
|
|
// analyzeKmzFile(`${prexUrl2.value}?_t=${new Date().getTime()}`).then(async res => {
|
// const templateXML = await res.fileInfoObj['wpmz/template.kml']
|
// const templateXMLJSON = XMLToJSON(templateXML)?.['Document']
|
// const templateXMLObj = removeTextKey(templateXMLJSON.Folder)
|
// renderingLine(templateXMLObj)
|
// })
|
// // })
|
// }
|
|
// 异步解析kmz文件
|
const analysis = async url => {
|
return new Promise(async resolve => {
|
const res = await analyzeKmzFile(`${url}?_t=${new Date().getTime()}`)
|
const templateXML = await res.fileInfoObj['wpmz/template.kml']
|
const templateXMLJSON = XMLToJSON(templateXML)?.['Document']
|
const templateXMLObj = removeTextKey(templateXMLJSON.Folder)
|
resolve(templateXMLObj)
|
})
|
}
|
|
// 绘制线和飞行
|
const drawLine = async () => {
|
let prexUrl = ref(import.meta.env.VITE_APP_AIRLINE_URL+ props.wayLineFile);
|
const res = await analysis(prexUrl.value);
|
renderingLine(res);
|
const points = res.Placemark.map(item => item.Point.coordinates.split(','))
|
flyToPoints(points)
|
}
|
|
watch(() => props.wayLineFile, async (newValue, oldValue) => {
|
if(newValue){
|
await removeMap();
|
await init();
|
await drawLine();
|
}
|
})
|
|
const removeMap = () => {
|
viewer.entities.removeAll()
|
viewer.destroy()
|
}
|
|
|
onBeforeUnmount(() => {
|
removeMap()
|
})
|
|
onMounted(() => {
|
nextTick(() => {
|
init()
|
})
|
})
|
</script>
|
<style scoped lang="scss">
|
#taskMap {
|
height: 100%;
|
|
:deep() {
|
.cesium-viewer {
|
height: 100%;
|
overflow: hidden;
|
|
.cesium-viewer-cesiumWidgetContainer {
|
width: 100%;
|
height: 100%;
|
|
.cesium-widget {
|
width: 100%;
|
height: 100%;
|
|
canvas {
|
width: 100%;
|
height: 100%;
|
}
|
}
|
}
|
}
|
|
.cesium-viewer-bottom {
|
display: none;
|
}
|
}
|
}
|
</style>
|