From c7c9de0214cb1e6ad3d29c8a3a0c7a8ea31fbaaa Mon Sep 17 00:00:00 2001
From: GuLiMmo <2820890765@qq.com>
Date: Tue, 05 Dec 2023 15:37:13 +0800
Subject: [PATCH] 航线点位事件编辑完善

---
 src/hooks/use-cesium-tsa.ts |   38 +++++++++++++++++++++++---------------
 1 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/src/hooks/use-cesium-tsa.ts b/src/hooks/use-cesium-tsa.ts
index b5c5273..dacf74c 100644
--- a/src/hooks/use-cesium-tsa.ts
+++ b/src/hooks/use-cesium-tsa.ts
@@ -46,7 +46,7 @@
 // 定义全局的viewer变量防止重复生成
 // eslint-disable-next-line no-var
 var viewer: Cesium.Viewer | null = null
-export function cesiumOperation() {
+export function cesiumOperation () {
   let handler: Cesium.ScreenSpaceEventHandler
   const TDT_Token = 'c6eea7dad4fa1e2d1e32ec0e7c9735db'
   // 天地图Key
@@ -146,12 +146,12 @@
     }
   }
 
-  function removeAllDataSource() {
-    viewer.dataSources.removeAll()
+  function removeAllDataSource () {
+    viewer?.dataSources.removeAll()
   }
 
   // 清除所有标记点
-  function removeAllPoint() {
+  function removeAllPoint () {
     if (viewer) {
       viewer?.entities?.removeAll()
     }
@@ -184,16 +184,23 @@
       }
     })
   }
-
   // 添加点击事件
+  let leftClickHandler: Cesium.ScreenSpaceEventHandler
   const addClickEvent = (sid: string, cb: Function) => {
-    const handler = new Cesium.ScreenSpaceEventHandler(viewer?.scene.canvas)
-    handler.setInputAction(function (click: { position: Cesium.Cartesian2 }) {
+    if (leftClickHandler) {
+      removeClickEvent()
+    }
+    leftClickHandler = new Cesium.ScreenSpaceEventHandler(viewer?.scene.canvas)
+    leftClickHandler.setInputAction(function (click: { position: Cesium.Cartesian2 }) {
       const pick = viewer?.scene.pick(click.position)
-      if (pick && pick.id._id === sid) {
-        cb(click, pick, viewer)
+      if (pick && (pick.id._id === sid || pick.id._id.includes(sid))) {
+        cb(click, pick, viewer, handler)
       }
     }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
+  }
+  // 移除事件
+  const removeClickEvent = () => {
+    leftClickHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
   }
 
   // 添加线段
@@ -205,7 +212,7 @@
   }
 
   // 通过点ID删除
-  function removeById(id: string) {
+  function removeById (id: string) {
     viewer?.entities.removeById(id)
     const pointEntity = viewer?.entities.getById(id)
     if (pointEntity && pointEntity !== undefined) {
@@ -213,12 +220,12 @@
     }
   }
   // 通过点ID获取实体
-  function getEntityById(id: string) {
+  function getEntityById (id: string) {
     const pointEntity = viewer?.entities.getById(id)
     return pointEntity
   }
   // 更新图片实体位置
-  function updateEntityPosition(longitude: number, latitude: number, id: string, params?: any) {
+  function updateEntityPosition (longitude: number, latitude: number, id: string, params?: any) {
     const entity = getEntityById(id) as Cesium.Entity
     const position = Cesium.Cartesian3.fromDegrees(longitude, latitude)
     const heading = Cesium.Math.toRadians(-params.heading)
@@ -290,8 +297,8 @@
   // 加载json文件
   const loadGeoJson = (url: string) => {
     const option = {
-      stroke: Cesium.Color.HOTPINK,
-      fill: Cesium.Color.PINK,
+      stroke: Cesium.Color.HOTPINK.withAlpha(0.5),
+      fill: Cesium.Color.PINK.withAlpha(0.3),
       strokeWidth: 3,
     }
     const promise = Cesium.GeoJsonDataSource.load(url, option)
@@ -348,6 +355,7 @@
     addEllipse,
     loadGeoJson,
     removeAllDataSource,
-    addClickEvent
+    addClickEvent,
+    removeClickEvent
   }
 }

--
Gitblit v1.9.3