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 } }