From 52a426373c619415f876952b0de04af82564dd96 Mon Sep 17 00:00:00 2001
From: GuLiMmo <2820890765@qq.com>
Date: Wed, 06 Mar 2024 18:04:02 +0800
Subject: [PATCH] update:航线全局设置编辑、新增航点、事件编辑等

---
 src/utils/cesium/use-map-draw.ts |  108 +++++++++++++++++++++++++++++++++--------------------
 1 files changed, 67 insertions(+), 41 deletions(-)

diff --git a/src/utils/cesium/use-map-draw.ts b/src/utils/cesium/use-map-draw.ts
index 3bba15f..3734bd9 100644
--- a/src/utils/cesium/use-map-draw.ts
+++ b/src/utils/cesium/use-map-draw.ts
@@ -86,6 +86,29 @@
   },
 ]
 
+const waylinePointsEvent = ref<tragetPoint[]>([])
+// 路线详情
+const waylineDetails = reactive<waylineDetails[]>([
+  {
+    title: '航线长度',
+    value: '0',
+  },
+  {
+    title: '预计执行时间',
+    value: '0',
+  },
+  {
+    title: '航点',
+    value: 0,
+  },
+  {
+    title: '照片',
+    value: 0,
+  },
+])
+// kml中的全部实体
+const kmlEntities = ref<Cesium.Entity[]>([])
+
 const useMapDraw = (
   dataSource: Cesium.DataSource | any,
   entitiesList: Cesium.Entity[] | any,
@@ -93,43 +116,29 @@
   global: any,
 ) => {
   const ellipsoid = global.$viewer.scene.globe.ellipsoid
-  const waylinePointsEvent = ref<tragetPoint[]>([])
-  // 路线详情
-  const waylineDetails = reactive<waylineDetails[]>([
-    {
-      title: '航线长度',
-      value: '0',
-    },
-    {
-      title: '预计执行时间',
-      value: '0',
-    },
-    {
-      title: '航点',
-      value: 0,
-    },
-    {
-      title: '照片',
-      value: 0,
-    },
-  ])
-  // kml中的全部实体
-  const kmlEntities = ref<Cesium.Entity[]>([])
+
   //   kml文件中点位string
   let kmlPoints: string[] | any = []
-  //   记录点位信息
-  const cartesianArr: Cesium.Cartesian3[] = []
+  //  记录点位信息
+  let cartesianArr: Cesium.Cartesian3[] = []
 
   //   绘制路线
-  const drawWayline = async () => {
-    dataSource.show = false
-    let kmlEntityArr = [...entitiesList]
+  const drawWayline = async (entities: Cesium.Entity[], kmlStr: string) => {
+    if (!entities && !kmlStr) {
+      dataSource.show = false
+    }
+    let kmlEntityArr = entities || [...entitiesList]
     if (dataSource) {
       kmlEntityArr = dataSource.entities.values[0]._children
       kmlEntities.value = kmlEntityArr
     }
     // 获取所有点位
-    const kmlRes = await analyzeKmzFile(fileUrl)
+    let kmlRes = ''
+    if (kmlStr) {
+      kmlRes = kmlStr
+    } else {
+      kmlRes = await analyzeKmzFile(fileUrl)
+    }
     // 所有航点
     kmlPoints = getKmlParams(kmlRes, false, {
       name: 'Placemark',
@@ -138,6 +147,11 @@
     })
     // 起飞点
     let btmStartPoint = null
+    // 判断是否存在实体集
+    if (entities) {
+      cartesianArr = []
+      waylinePointsEvent.value = []
+    }
     // 修改点位样式
     for (let i = 0; i < kmlEntityArr.length; i++) {
       const entity: any = kmlEntityArr[i]
@@ -148,21 +162,17 @@
       const latitude = Cesium.Math.toDegrees(c2Postion.latitude)
       // 获取当前航点中的值
       const point = kmlPoints[i]
-      const getPointHeight: any = getKmlParams(point, true, {
+      const getHaeHeight: any = getKmlParams(point, true, {
         name: 'ellipsoidHeight',
         findRegx: '([\\s\\S]*?)',
       })
-      const getHaeHeight: any = getKmlParams(point, true, {
+      const getPointHeight: any = getKmlParams(point, true, {
         name: 'height',
         findRegx: '([\\s\\S]*?)',
       })
       const AslHeight = Number(getPointHeight[1])
       const HeaHeight = Number(getHaeHeight[1])
-      entity.position = Cesium.Cartesian3.fromDegrees(
-        longitude,
-        latitude,
-        AslHeight,
-      )
+      entity.position = Cesium.Cartesian3.fromDegrees(longitude, latitude, AslHeight)
       if (i === 0) {
         btmStartPoint = Cesium.Cartesian3.fromDegrees(longitude, latitude, 0)
       }
@@ -191,6 +201,9 @@
         color: Cesium.Color.GHOSTWHITE,
         outlineColor: Cesium.Color.BLACK,
       })
+      if (entities) {
+        global.$viewer.entities.add(entity)
+      }
       // 创建虚线
       addPolyline({
         id: 'dashLine' + i,
@@ -244,7 +257,9 @@
     })
     // 获取航线详情
     getWaylineDetails(kmlRes)
-    dataSource.show = true
+    if (!entities && !kmlStr) {
+      dataSource.show = true
+    }
     global.$viewer.flyTo(getEntityById('entityLine'), {
       offset: new Cesium.HeadingPitchRange(0, -90, 600),
     })
@@ -267,15 +282,19 @@
     const sportTime = polylineLength / speed
     // 判断有没有超过一分钟
     const time = sportTime / 60
-    waylineDetails[1].value =
-      Math.trunc(time) + ' m ' + Math.round((time % 1) * 60) + ' s'
+    waylineDetails[1].value = Math.trunc(time) + ' m ' + Math.round((time % 1) * 60) + ' s'
 
     // 获取事件
-    getPointEvent()
+    getPointEvent(kmlStr || null)
   }
   // 获取航线点位事件
-  const getPointEvent = async () => {
-    const kmlRes = await analyzeKmzFile(fileUrl)
+  const getPointEvent = async (kmlStr: string | null) => {
+    let kmlRes = ''
+    if (kmlStr) {
+      kmlRes = kmlStr
+    } else {
+      await analyzeKmzFile(fileUrl)
+    }
     // 所有航点
     const points = getKmlParams(kmlRes, false, {
       name: 'Placemark',
@@ -347,6 +366,10 @@
       }
     })
   }
+  const clearWaylineData = () => {
+    kmlEntities.value = []
+    waylinePointsEvent.value = []
+  }
 
   return {
     waylinePointsEvent,
@@ -354,6 +377,9 @@
     kmlEntities,
     drawWayline,
     getWaylineDetails,
+    clearWaylineData,
   }
 }
+
+export { waylinePointsEvent, waylineDetails, kmlEntities }
 export default useMapDraw

--
Gitblit v1.9.3