feat: 添加 Cesium Viewer 组件及相关功能,优化项目结构和配置
This commit is contained in:
100
src/components/h-cesium-viewer/h-cesium-viewer-class.ts
Normal file
100
src/components/h-cesium-viewer/h-cesium-viewer-class.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user