feat: 添加 Cesium Viewer 组件及相关功能,优化项目结构和配置
All checks were successful
/ build-and-deploy-to-vercel (push) Successful in 2m45s
/ lint-build-and-check (push) Successful in 4m30s
/ surge (push) Successful in 2m57s
/ playwright (push) Successful in 6m56s

This commit is contained in:
严浩
2025-03-31 19:43:52 +08:00
parent 32bc83e16e
commit 4636f9fde4
13 changed files with 387 additions and 13 deletions

View File

@ -0,0 +1,100 @@
import * as Cesium from 'cesium';
import { VIEWER_OPTIONS_FN } from './VIEWER_OPTIONS';
export interface GroundStationOptions {
// 调整顺序以符合 ESLint 规则
color?: Cesium.Color; // 点的可选颜色
height?: number; // 可选高度默认为0
id: string; // 站点的唯一标识符
latitude: number;
longitude: number;
name: string;
pixelSize?: number; // 点的可选像素大小
}
export class HCesiumViewerCls {
viewer: Cesium.Viewer | null = null;
/**
* 初始化 Cesium Viewer
* @param container - 用于承载 Cesium Viewer 的 DOM 元素或其 ID
*/
initCesiumViewer(container: ConstructorParameters<typeof Cesium.Viewer>[0]) {
this.viewer = new Cesium.Viewer(container, VIEWER_OPTIONS_FN());
}
/**
* 向视图中添加地面站实体
* @param options - 地面站的选项参数
* @returns 添加的实体对象,如果视图未初始化则返回 undefined
*/
addGroundStation(options: GroundStationOptions): Cesium.Entity | undefined {
if (!this.viewer) {
console.error('视图未初始化。请先调用 initCesiumViewer 方法。');
return;
}
const {
// 调整解构赋值顺序
// color = Cesium.Color.RED, // 默认颜色 - 已移除,将使用随机颜色
height = 0, // 如果未提供默认高度为0
id, // 获取 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,
label: {
font: '14pt sans-serif',
outlineWidth: 2,
pixelOffset: new Cesium.Cartesian2(0, -12), // 标签略微偏移到点的上方
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
text: name,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
},
name: name,
point: {
color: randomColor, // 使用随机颜色
outlineColor: Cesium.Color.WHITE,
outlineWidth: 2,
pixelSize,
},
position: position,
});
return this.viewer.entities.add(groundStationEntity);
}
/**
* 从视图中移除指定的地面站实体
* @param entity - 要移除的地面站实体对象 (由 addGroundStation 返回)
* @returns 如果成功移除则返回 true否则返回 false
*/
removeGroundStation(entity: Cesium.Entity): boolean {
if (!this.viewer) {
console.error('视图未初始化。无法移除地面站。');
return false;
}
return this.viewer.entities.remove(entity);
}
/**
* 销毁 Cesium Viewer 实例并清理资源
*/
destroy() {
if (this.viewer) {
this.viewer.destroy();
this.viewer = null;
}
}
}