import { useEntity } from '@/hooks/entity' import { getDirection } from '@/utils/pos' export function useMultiZBTraj() { return { multiZBTrajMap, addMultiZBTraj, removeAllMultiZBTraj, } } const multiZBTrajMap = new Map() const { getMBEntityOpt } = useEntity() // 添加多目标目标和轨迹 function addMultiZBTraj(zbList) { removeAllMultiZBTraj() Object.keys(zbList).forEach(async id => { if (!multiZBTrajMap.has(id)) { const { info, list: trajList } = zbList[id] const { targetType, country, extendInfo } = info const ZBOpt = await getMBEntityOpt({ id, targetType, country, extendInfo, }) const startTime = trajList[0].target_time const endTime = trajList.at(-1).target_time const positions = getPositions(trajList, targetType) ZBOpt.billboard.rotation = new Cesium.CallbackProperty(currentTime => { const point1 = positions.position.getValue(currentTime) const nextTime = Cesium.JulianDate.addSeconds( currentTime, 2, new Cesium.JulianDate() ) const point2 = positions.position.getValue(nextTime) if (point2 && point1) { const heading = getDirection(point1, point2) const angleDegrees = Cesium.Math.toDegrees(heading) return Cesium.Math.toRadians(angleDegrees) } }, false) Object.assign(ZBOpt, { ...getPositions(trajList, targetType), ...getPath(startTime, endTime), // ...getAvailability(startTime, endTime), }) console.log('ZBOpt', ZBOpt) const ZBEntity = viewer.entities.add(ZBOpt) multiZBTrajMap.set(id, ZBEntity) } }) } function getPositions(trajList, targetType) { const positionProperty = new Cesium.SampledPositionProperty() trajList.forEach(({ target_time, target_lon, target_lat }, index) => { const time = Cesium.JulianDate.fromDate(new Date(target_time)) const position = Cesium.Cartesian3.fromDegrees( target_lon, target_lat, window.settings.mbDict[targetType].height ) positionProperty.addSample(time, position) // viewer.entities.add({ // position, // point: { // pixelSize: 3, // color: Cesium.Color.RED, // }, // }) }) positionProperty.setInterpolationOptions({ interpolationDegree: 2, interpolationAlgorithm: Cesium.HermitePolynomialApproximation, }) return { position: positionProperty, orientation: new Cesium.VelocityOrientationProperty(positionProperty), } } function getPath(startTime, endTime) { console.log(new Date(startTime).getTime() - new Date(endTime).getTime()) return { path: new Cesium.PathGraphics({ width: 0.5, // show: true, resolution: 1, leadTime: new Date(startTime).getTime() - new Date(endTime).getTime(), trailTime: new Date(startTime).getTime() - new Date(endTime).getTime(), material: Cesium.Color.fromRandom().withAlpha(1), }), } } function getAvailability(startTime, endTime) { const start = Cesium.JulianDate.fromDate(new Date(startTime)) const stop = Cesium.JulianDate.fromDate(new Date(endTime)) return { availability: new Cesium.TimeIntervalCollection([ new Cesium.TimeInterval({ start: start, stop: stop }), ]), } } function removeAllMultiZBTraj() { for (let [key, entity] of multiZBTrajMap.entries()) { viewer.entities.remove(entity) multiZBTrajMap.delete(key) } }