From 5f35a4891178ece4fc611ecb515610841abe0f43 Mon Sep 17 00:00:00 2001
From: xieb <vip_xiaobin810@163.com>
Date: Tue, 21 Nov 2023 17:22:45 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/demo' into demo

---
 src/hooks/use-cesium-tsa.ts |   27 ++++++++++++++++++---------
 1 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/hooks/use-cesium-tsa.ts b/src/hooks/use-cesium-tsa.ts
index b5c5273..77c6b20 100644
--- a/src/hooks/use-cesium-tsa.ts
+++ b/src/hooks/use-cesium-tsa.ts
@@ -146,12 +146,12 @@
     }
   }
 
-  function removeAllDataSource() {
-    viewer.dataSources.removeAll()
+  function removeAllDataSource () {
+    viewer?.dataSources.removeAll()
   }
 
   // 清除所有标记点
-  function removeAllPoint() {
+  function removeAllPoint () {
     if (viewer) {
       viewer?.entities?.removeAll()
     }
@@ -185,15 +185,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)
+        cb(click, pick, viewer, handler)
       }
     }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
+  }
+  // 移除事件
+  const removeClickEvent = () => {
+    leftClickHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
   }
 
   // 添加线段
@@ -290,8 +298,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 +356,7 @@
     addEllipse,
     loadGeoJson,
     removeAllDataSource,
-    addClickEvent
+    addClickEvent,
+    removeClickEvent
   }
 }

--
Gitblit v1.9.3