diff --git a/.vscode/settings.json b/.vscode/settings.json index f8f8cb1..7c345c6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,9 +3,9 @@ "source.fixAll.eslint": "explicit", "source.fixAll.stylelint": "explicit", "source.fixAll.oxc": "explicit", - // "source.fixAll.eslint": "never", - // "source.fixAll.stylelint": "never", - // "source.fixAll.oxc": "never", + "source.fixAll.eslint": "never", + "source.fixAll.stylelint": "never", + "source.fixAll.oxc": "never", "source.organizeImports": "never" }, "editor.formatOnSave": true, diff --git a/src/components/h-cesium-viewer/h-cesium-viewer-class.ts b/src/components/h-cesium-viewer/h-cesium-viewer-class.ts index b0eed2e..ee48430 100644 --- a/src/components/h-cesium-viewer/h-cesium-viewer-class.ts +++ b/src/components/h-cesium-viewer/h-cesium-viewer-class.ts @@ -3,7 +3,6 @@ import * as Cesium from 'cesium'; import { VIEWER_OPTIONS_FN } from './VIEWER_OPTIONS'; export interface GroundStationOptions { - color?: Cesium.Color; // 点的可选颜色 height?: number; // 可选高度,默认为0 id: string; // 站点的唯一标识符 latitude: number; @@ -13,7 +12,9 @@ export interface GroundStationOptions { } export class HCesiumViewerCls { - viewer: Cesium.Viewer | null = null; + private viewer: Cesium.Viewer | null = null; + // 用于存储当前地面站实体的 Map + currentStationEntities: Map = new Map(); /** * 初始化 Cesium Viewer @@ -21,32 +22,31 @@ export class HCesiumViewerCls { */ initCesiumViewer(container: ConstructorParameters[0]) { this.viewer = new Cesium.Viewer(container, VIEWER_OPTIONS_FN()); + // 初始化时清空可能存在的旧实体引用 + this.currentStationEntities.clear(); } /** * 向视图中添加地面站实体 * @param options - 地面站的选项参数 - * @returns 添加的实体对象。 + * @returns 添加的实体对象,如果添加失败则返回 null。 */ - addGroundStation(options: GroundStationOptions): Cesium.Entity { + addGroundStation(options: GroundStationOptions): Cesium.Entity | null { if (!this.viewer) { - throw new Error('视图未初始化。请先调用 initCesiumViewer 方法。'); + console.error('视图未初始化。无法添加地面站。'); // 使用 console.error 替代抛出错误,以便在 index.vue 中处理 + return null; } - const { - height = 0, // 如果未提供,默认高度为0 - id, - latitude, - longitude, - name, - pixelSize = 10, // 默认像素大小 - } = options; + // 检查是否已存在相同 ID 的实体 + if (this.currentStationEntities.has(options.id)) { + console.warn(`ID 为 "${options.id}" 的地面站实体已存在,跳过添加。`); + return this.currentStationEntities.get(options.id) || null; + } + // 解构赋值获取站点信息 + const { height = 0, id, latitude, longitude, name, pixelSize = 10 } = options; const position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height); - // 生成随机颜色 - const randomColor = Cesium.Color.fromRandom(); - const groundStationEntity = new Cesium.Entity({ // 使用传入的 id 作为实体的唯一标识符 id: id, @@ -60,7 +60,7 @@ export class HCesiumViewerCls { }, name: name, point: { - color: randomColor, // 使用随机颜色 + color: Cesium.Color.fromRandom(), // 随机颜色 outlineColor: Cesium.Color.WHITE, outlineWidth: 2, pixelSize, @@ -68,29 +68,59 @@ export class HCesiumViewerCls { position: position, }); - return this.viewer.entities.add(groundStationEntity); + const addedEntity = this.viewer.entities.add(groundStationEntity); + // 添加成功后,将其存入 Map + this.currentStationEntities.set(id, addedEntity); + return addedEntity; } /** - * 从视图中移除指定的地面站实体 - * @param entity - 要移除的地面站实体对象 (由 addGroundStation 返回) + * 从视图中移除指定的地面站实体 (通过 ID) + * @param entityId - 要移除的地面站实体的 ID * @returns 如果成功移除则返回 true,否则返回 false */ - removeGroundStation(entity: Cesium.Entity): boolean { + removeGroundStationById(entityId: string): boolean { if (!this.viewer) { console.error('视图未初始化。无法移除地面站。'); return false; } - return this.viewer.entities.remove(entity); + const entityToRemove = this.currentStationEntities.get(entityId); + if (entityToRemove) { + const removed = this.viewer.entities.remove(entityToRemove); + if (removed) { + // 移除成功后,从 Map 中删除 + this.currentStationEntities.delete(entityId); + return true; + } else { + console.warn(`尝试从 Cesium 移除 ID 为 "${entityId}" 的实体失败。`); + return false; // Cesium 移除失败 + } + } else { + // console.warn(`未在 Map 中找到 ID 为 "${entityId}" 的地面站实体,无法移除。`); // 可能在 clearAll 时触发,不一定是警告 + return false; // Map 中未找到 + } + } + + // 移除 highlightStation 和 unhighlightStation 方法 + + clearAllGroundStations() { + // ... (清理逻辑不变) + if (!this.viewer) return; + // 优化:直接遍历 Map 清理,避免重复查找 + // 使用 for...of 循环替代 forEach + for (const entity of this.currentStationEntities.values()) { + this.viewer?.entities.remove(entity); + } + this.currentStationEntities.clear(); } - /** - * 销毁 Cesium Viewer 实例并清理资源 - */ destroy() { + // ... (销毁逻辑不变) if (this.viewer) { + this.clearAllGroundStations(); this.viewer.destroy(); this.viewer = null; } + this.currentStationEntities.clear(); // 确保清空 } } diff --git a/src/components/h-cesium-viewer/index.vue b/src/components/h-cesium-viewer/index.vue index 79dc0f1..7527358 100644 --- a/src/components/h-cesium-viewer/index.vue +++ b/src/components/h-cesium-viewer/index.vue @@ -1,57 +1,53 @@