forked from drone/command-center-dashboard

张含笑
2025-04-16 b640f46407c19123f95565a5cc597e65cf6d76e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as Cesium from 'cesium'
/**
 * 地图事件 Hook
 * @param {Cesium.Viewer} viewer - Cesium 实例
 * @param {Object} options - 配置选项
 * @param {Function} options.onEventClick - 点击事件的回调
 */
export function useMapEvents (viewer, options = {}) {
  const { onEventClick } = options
  let handler = null
 
  // 查找特定类型的实体
  const findEntityByType = (entities, type) => {
    return entities.find(entity =>
      entity?.properties?.customData?._value?.data?.type === type
    )
  }
 
  // 左键单机事件
  const singleMachineEvent = async click => {
    let clickedEntities = viewer.scene.drillPick(click.position).map(item => item.id)
    if (!clickedEntities.length) return
 
    const currentEntity = findEntityByType(clickedEntities, 'device') ||
      findEntityByType(clickedEntities, 'event')
 
    currentEntity = deviceAggregationFind || deviceFind || eventFind
 
    if (currentEntity) {
      onEventClick?.(currentEntity)
    }
  }
 
  const handlerInit = () => {
    if (handler) return
 
    handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
    handler.setInputAction(singleMachineEvent, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  }
 
  const removeHandler = () => {
    handler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    handler?.destroy()
    handler = null
  }
 
  // 自动清理
  onUnmounted(() => {
    removeHandler()
  })
 
  return {
    handlerInit,
    removeHandler
  }
}