refactor: 重构 Cesium 相关代码
All checks were successful
/ build-and-deploy-to-vercel (push) Successful in 2m53s
/ lint-build-and-check (push) Successful in 4m23s
/ surge (push) Successful in 3m14s
/ playwright (push) Successful in 5m51s

This commit is contained in:
严浩
2025-04-01 01:51:02 +08:00
parent 2c6a4287d2
commit 7072f171e0
5 changed files with 117 additions and 95 deletions

View File

@ -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<string, Cesium.Entity> = new Map();
/**
* 初始化 Cesium Viewer
@ -21,32 +22,31 @@ export class HCesiumViewerCls {
*/
initCesiumViewer(container: ConstructorParameters<typeof Cesium.Viewer>[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(); // 确保清空
}
}