Files
xian_vue_new/src/utils/cesium/CesiumUtils.ts
T

578 lines
18 KiB
TypeScript
Raw Normal View History

import type { CesiumInitOptions } from '@/types/cesium/CesiumInitOptions';
import type { EntityOptions } from '@/types/cesium/EntityOptions';
import type { PrimitiveOptions } from '@/types/cesium/PrimitiveOptions';
import type { LayerConfig } from '@/types/cesium/LayerConfig';
import type {
CustomizeGeoJsonDataSource,
GeoJsonOptions,
} from '@/types/cesium/GeoJsonOptions';
import {
Viewer,
Entity,
DataSource,
ImageryLayer,
Primitive,
BillboardCollection,
Cartesian3,
ScreenSpaceEventHandler,
ScreenSpaceEventType,
Cartesian2,
SceneTransforms,
Rectangle,
} from 'cesium';
import { CesiumViewerManager } from './CesiumViewerManager';
import { EntityManager } from './EntityManager';
import { PrimitiveManager } from './PrimitiveManager';
import { LayerManager } from './LayerManager';
import { GeoJsonManager, type ClearType } from './GeoJsonManager';
import { CameraController } from './CameraController';
import config from '@/config/config.json';
import type { ClickObject } from '@/types/cesium/ClickObject';
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
// 导出 ClearType 类型
export type { ClearType };
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:59:00 +08:00
* Cesium 工具类(重构版 - 委托模式)
2026-04-07 20:01:06 +08:00
*/
export class CesiumUtils {
2026-04-08 18:59:00 +08:00
// 管理器实例
#viewerManager: CesiumViewerManager;
#entityManager: EntityManager | null = null;
#primitiveManager: PrimitiveManager | null = null;
#layerManager: LayerManager | null = null;
#geoJsonManager: GeoJsonManager | null = null;
#cameraController: CameraController | null = null;
2026-04-07 20:01:06 +08:00
constructor() {
this.#viewerManager = new CesiumViewerManager();
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 初始化 Cesium Viewer
2026-04-08 18:59:00 +08:00
* @param options - Viewer 初始化选项
* @param type - 底图类型:0=影像图,1=矢量图(默认 0)
2026-04-12 16:46:56 +08:00
* @param tdMapToken - 天地图 Token 数组(可选)
2026-04-07 20:01:06 +08:00
*/
async initCesiumViewer(
options: CesiumInitOptions,
type: number = 0,
tdMapToken?: string[]
): Promise<void> {
await this.#viewerManager.initCesiumViewer(options, type, tdMapToken);
2026-04-11 10:09:40 +08:00
const viewer = this.#viewerManager.getViewer();
2026-04-08 18:59:00 +08:00
if (viewer) {
this.#entityManager = new EntityManager(viewer);
this.#primitiveManager = new PrimitiveManager(viewer);
this.#layerManager = new LayerManager(viewer);
this.#geoJsonManager = new GeoJsonManager(viewer);
this.#cameraController = new CameraController(viewer);
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
}
/**
* 设置相机高度限制
* @param minHeight - 最小高度
* @param maxHeight - 最大高度
*/
setHeightLimits(
minHeight: number = config.camera.min,
maxHeight: number = config.camera.max
) {
this.#cameraController!.setHeightLimits(minHeight, maxHeight);
}
/**
* 清除相机高度限制
*/
clearHightLimits(): void {
this.#cameraController!.clearHeightLimits();
}
/**
* 监听相机移动结束事件,并判断相机是否超出指定矩形范围
* @param range - 矩形范围[左下角经纬度,右上角经纬度]
* @param duration - 飞行持续时间(秒),默认1秒
*/
outOverView(
range: [
[number, number],
[number, number],
] = config.defauleRectangleRange as [[number, number], [number, number]],
duration: number = 1
) {
const rectangle = Rectangle.fromDegrees(...range[0], ...range[1]);
this.#cameraController!.outOverView(rectangle, duration);
}
2026-04-08 18:17:54 +08:00
/**
* 销毁 Cesium Viewer
*/
destroyCesiumViewer(): void {
this.#viewerManager.destroyCesiumViewer(() =>
this.clearAllResources('all')
);
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
// ===================== 实体管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:59:00 +08:00
* 添加 Cesium 实体
* @param entityOptions - 实体配置选项
* @returns 创建的 Entity 实例
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
addCesiumEntity(entityOptions: EntityOptions): Entity {
this.#checkManager(this.#entityManager, 'EntityManager');
return this.#entityManager!.addCesiumEntity(entityOptions);
2026-04-07 20:01:06 +08:00
}
2026-04-09 15:52:56 +08:00
/**
* 批量添加实体
* @param entityOptionsList - 实体配置选项数组
* @returns 创建的 Entity 实例数组
*/
addCesiumEntitiesBatch(entityOptionsList: EntityOptions[]): Entity[] {
this.#checkManager(this.#entityManager, 'EntityManager');
return this.#entityManager!.addCesiumEntitiesBatch(entityOptionsList);
2026-04-09 15:52:56 +08:00
}
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 查询实体
2026-04-08 18:59:00 +08:00
* @param entityId - 实体 ID
* @returns Entity 实例,不存在则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
getCesiumEntityById(entityId: string): Entity | null {
this.#checkManager(this.#entityManager, 'EntityManager');
return this.#entityManager!.getCesiumEntityById(entityId);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 删除实体
2026-04-08 18:59:00 +08:00
* @param entityId - 实体 ID
* @returns 是否删除成功
2026-04-08 18:17:54 +08:00
*/
removeCesiumEntity(entityId: string): boolean {
this.#checkManager(this.#entityManager, 'EntityManager');
return this.#entityManager!.removeCesiumEntity(entityId);
2026-04-07 20:01:06 +08:00
}
/**
* 批量删除实体
2026-04-08 18:59:00 +08:00
* @param entityIds - 实体 ID 数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
batchRemoveCesiumEntities(entityIds: string[]): void {
this.#checkManager(this.#entityManager, 'EntityManager');
this.#entityManager!.batchRemoveCesiumEntities(entityIds);
2026-04-08 18:59:00 +08:00
}
/**
* 清除实体
* @param clearType - 清除类型:'default'=默认实体,'custom'=自定义实体,'all'=所有实体(默认 'custom'
*/
clearAllEntities(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#entityManager, 'EntityManager');
this.#entityManager!.clearAllEntities(clearType);
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有实体ID
* @param clearType - 类型:'default'=默认实体,'custom'=自定义实体,'all'=所有实体(默认 'all'
* @returns 实体 ID 集合
*/
getEntityIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#entityManager, 'EntityManager');
return this.#entityManager!.getEntityIds(clearType);
}
2026-04-08 18:17:54 +08:00
// ===================== Primitive 管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-09 15:52:56 +08:00
* 添加单个 Primitive
* @param primitive - Primitive 配置选项
*/
addPrimitive(primitive: PrimitiveOptions): void {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
this.#primitiveManager!.addPrimitive(primitive);
2026-04-09 15:52:56 +08:00
}
/**
* 批量添加 Primitive(优化版本)
* - 按类型分组后批量创建,减少 scene.primitives.add 调用次数
* - 同类型的多个实例合并到一个 Primitive 或 BillboardCollection 中
2026-04-08 18:59:00 +08:00
* @param primitives - Primitive 配置选项数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
addPrimitivesBatch(primitives: PrimitiveOptions[]): void {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
this.#primitiveManager!.addPrimitivesBatch(primitives);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 查询 Primitive
2026-04-08 18:59:00 +08:00
* @param id - Primitive ID
* @returns Primitive 或 BillboardCollection 实例,不存在则返回 undefined
2026-04-08 18:17:54 +08:00
*/
getPrimitiveById(id: string): Primitive | BillboardCollection | undefined {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
return this.#primitiveManager!.getPrimitiveById(id);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 删除 Primitive
2026-04-08 18:59:00 +08:00
* @param id - Primitive ID
* @returns 是否删除成功
2026-04-08 18:17:54 +08:00
*/
removePrimitiveById(id: string): boolean {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
return this.#primitiveManager!.removePrimitiveById(id);
2026-04-08 18:59:00 +08:00
}
2026-04-08 18:17:54 +08:00
2026-04-08 18:59:00 +08:00
/**
* 清除 Primitive
* @param clearType - 清除类型:'default'=默认 Primitive'custom'=自定义 Primitive'all'=所有 Primitive(默认 'custom'
*/
clearAllPrimitives(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
this.#primitiveManager!.clearAllPrimitives(clearType);
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有Primitive ID
* @param clearType - 类型:'default'=默认 Primitive'custom'=自定义 Primitive'all'=所有 Primitive(默认 'all'
* @returns Primitive ID 集合
*/
getPrimitiveIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager');
return this.#primitiveManager!.getPrimitiveIds(clearType);
}
2026-04-08 18:17:54 +08:00
// ===================== 图层管理 =====================
2026-04-09 15:52:56 +08:00
/**
* 批量创建图层
* @param layerConfigs - 图层配置数组
* @returns 创建的 ImageryLayer 实例数组(失败的为 null)
*/
createLayersBatch(layerConfigs: LayerConfig[]): (ImageryLayer | null)[] {
this.#checkManager(this.#layerManager, 'LayerManager');
return this.#layerManager!.createLayersBatch(layerConfigs);
2026-04-09 15:52:56 +08:00
}
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 创建图层
2026-04-08 18:59:00 +08:00
* @param layerConfig - 图层配置
* @returns 创建的 ImageryLayer 实例,失败则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
createLayer(layerConfig: LayerConfig): ImageryLayer | null {
this.#checkManager(this.#layerManager, 'LayerManager');
return this.#layerManager!.createLayer(layerConfig);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 查询图层
2026-04-08 18:59:00 +08:00
* @param key - 图层 key
* @returns ImageryLayer 实例,不存在则返回 undefined
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
getLayerByKey(key: string): ImageryLayer | undefined {
this.#checkManager(this.#layerManager, 'LayerManager');
return this.#layerManager!.getLayerByKey(key);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 删除图层
2026-04-08 18:59:00 +08:00
* @param key - 图层 key
* @returns 是否删除成功
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
removeLayerByKey(key: string): boolean {
this.#checkManager(this.#layerManager, 'LayerManager');
return this.#layerManager!.removeLayerByKey(key);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 批量删除图层
2026-04-08 18:59:00 +08:00
* @param layerIds - 图层 ID 数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
batchRemoveLayers(layerIds: string[]): void {
this.#checkManager(this.#layerManager, 'LayerManager');
this.#layerManager!.batchRemoveLayers(layerIds);
2026-04-08 18:59:00 +08:00
}
/**
* 清除图层
* @param clearType - 清除类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'custom'
*/
clearAllLayers(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#layerManager, 'LayerManager');
this.#layerManager!.clearAllLayers(clearType);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
/**
* 获取所有图层 Key
* @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all'
* @returns 图层 Key 集合
*/
getLayerKeys(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#layerManager, 'LayerManager');
return this.#layerManager!.getLayerKeys(clearType);
}
2026-04-08 18:17:54 +08:00
// ===================== GeoJSON 图层管理 =====================
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 添加 GeoJSON 图层
2026-04-08 18:59:00 +08:00
* @param layerId - 图层唯一标识
* @param geojsonData - GeoJSON 数据(路径、URL 或对象)
* @param options - 配置选项(样式、标签等)
* @returns Promise<DataSource> 数据源实例
2026-04-08 18:17:54 +08:00
*/
async addGeoJsonLayer(
layerId: string,
geojsonData: CustomizeGeoJsonDataSource,
options?: GeoJsonOptions
): Promise<DataSource> {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.addGeoJsonLayer(layerId, geojsonData, options);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 根据ID查询图层
* @param layerId - 图层 ID
* @returns DataSource 实例,不存在则返回 undefined
*/
2026-04-08 18:17:54 +08:00
getGeoJsonLayerById(layerId: string): DataSource | undefined {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.getGeoJsonLayerById(layerId);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 删除图层
* @param layerId - 图层 ID
* @returns 是否删除成功
*/
2026-04-08 18:17:54 +08:00
removeGeoJsonLayer(layerId: string): boolean {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.removeGeoJsonLayer(layerId);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 批量添加GeoJSON图层
2026-04-09 15:52:56 +08:00
* @param layerConfigs - 图层配置数组,每个元素包含 layerId、geojsonData、isDefault 和 options
2026-04-08 18:59:00 +08:00
*/
async batchAddGeoJsonLayers(
2026-04-09 15:52:56 +08:00
layerConfigs: Array<{
layerId: string;
geojsonData: CustomizeGeoJsonDataSource;
isDefault?: boolean;
options?: GeoJsonOptions;
2026-04-09 15:52:56 +08:00
}>
): Promise<void> {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
await this.#geoJsonManager!.batchAddGeoJsonLayers(layerConfigs);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 批量删除
* @param layerIds - 图层 ID 数组
*/
2026-04-08 18:17:54 +08:00
batchRemoveGeoJsonLayers(layerIds: string[]): void {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
this.#geoJsonManager!.batchRemoveGeoJsonLayers(layerIds);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 清空图层
* @param clearType - 清除类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'custom'
*/
clearAllGeoJsonLayers(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
this.#geoJsonManager!.clearAllGeoJsonLayers(clearType);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 显示图层
* @param layerId - 图层 ID
* @returns 是否操作成功
*/
2026-04-08 18:17:54 +08:00
showGeoJsonLayer(layerId: string): boolean {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.showGeoJsonLayer(layerId);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 隐藏图层
* @param layerId - 图层 ID
* @returns 是否操作成功
*/
2026-04-08 18:17:54 +08:00
hideGeoJsonLayer(layerId: string): boolean {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.hideGeoJsonLayer(layerId);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
2026-04-08 18:59:00 +08:00
* 切换显隐
* @param layerId - 图层 ID
* @returns 切换后的显示状态,图层不存在则返回 null
2026-04-08 18:17:54 +08:00
*/
2026-04-08 18:59:00 +08:00
toggleGeoJsonLayer(layerId: string): boolean | null {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.toggleGeoJsonLayer(layerId);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 批量显示
* @param layerIds - 图层 ID 数组
* @returns 成功显示的图层数量
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:59:00 +08:00
batchShowGeoJsonLayers(layerIds: string[]): number {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.batchShowGeoJsonLayers(layerIds);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
2026-04-08 18:59:00 +08:00
* 批量隐藏
* @param layerIds - 图层 ID 数组
* @returns 成功隐藏的图层数量
2026-04-08 18:17:54 +08:00
*/
2026-04-08 18:59:00 +08:00
batchHideGeoJsonLayers(layerIds: string[]): number {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.batchHideGeoJsonLayers(layerIds);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 获取显示状态
* @param layerId - 图层 ID
* @returns 显示状态,图层不存在则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:59:00 +08:00
getGeoJsonLayerVisibility(layerId: string): boolean | null {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.getGeoJsonLayerVisibility(layerId);
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有 GeoJSON 图层 ID
* @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all'
* @returns GeoJSON 图层 ID 集合
*/
getGeoJsonLayerIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager');
return this.#geoJsonManager!.getGeoJsonLayerIds(clearType);
}
2026-04-11 10:09:40 +08:00
// ===================== 图层操作 =====================
/**
* 监听点击事件
*/
clickLayer(callback: (pickedObject: ClickObject) => void) {
2026-04-11 10:09:40 +08:00
const handler = new ScreenSpaceEventHandler(this.getViewer()?.scene.canvas);
handler.setInputAction((clickEvent: { position: Cartesian2 }) => {
2026-04-11 10:09:40 +08:00
// 在点击位置进行拾取
const pickedObject = CesiumUtilsSingleton.getViewer()?.scene.pick(
clickEvent.position
);
2026-04-11 10:09:40 +08:00
callback(pickedObject);
}, ScreenSpaceEventType.LEFT_CLICK);
}
2026-04-08 18:17:54 +08:00
// ===================== 视角控制 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 飞行到目标位置
2026-04-08 18:59:00 +08:00
* @param target - 目标位置 [经度, 纬度, 高度] 或 Cartesian3
* @param duration - 飞行持续时间(秒,默认 2)
2026-04-07 20:01:06 +08:00
*/
flyToTarget(
target: [number, number, number] | Cartesian3,
duration = 2
): void {
this.#checkManager(this.#cameraController, 'CameraController');
this.#cameraController!.flyToTarget(target, duration);
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 跳转到目标位置
* @param target - 目标位置 [经度, 纬度, 高度] 或 Cartesian3
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
viewToTarget(target: [number, number, number] | Cartesian3): void {
this.#checkManager(this.#cameraController, 'CameraController');
this.#cameraController!.viewToTarget(target);
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
// ===================== 清除与资源管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 清除所有资源
2026-04-08 18:59:00 +08:00
* @param clearType - 清除类型:'default'=默认资源,'custom'=自定义资源,'all'=所有资源(默认 'custom'
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
clearAllResources(clearType: ClearType = 'custom'): void {
this.clearAllEntities(clearType);
this.clearAllPrimitives(clearType);
this.clearAllLayers(clearType);
this.clearAllGeoJsonLayers(clearType);
2026-04-08 18:17:54 +08:00
}
// ===================== getter 和 setter函数 =====================
2026-04-08 18:59:00 +08:00
/**
* 获取 Viewer 实例
* @returns Viewer 实例,如果未初始化则返回 null
*/
2026-04-08 18:17:54 +08:00
getViewer(): Viewer | null {
return this.#viewerManager.getViewer();
2026-04-08 18:17:54 +08:00
}
// ===================== 辅助函数 =====================
2026-04-08 18:59:00 +08:00
/**
* 转换位置坐标
* @param pos - 位置坐标
* @returns Cartesian3 坐标
*/
2026-04-07 20:01:06 +08:00
convertPosition(pos: Cartesian3 | [number, number, number]): Cartesian3 {
return Array.isArray(pos)
? Cartesian3.fromDegrees(pos[0], pos[1], pos[2] || 0)
: pos;
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:59:00 +08:00
/**
* 批量转换位置坐标
* @param positions - 位置坐标数组
* @returns Cartesian3 坐标数组
*/
convertPositionArray(
positions: (Cartesian3 | [number, number, number])[]
): Cartesian3[] {
return positions.map((pos) => this.convertPosition(pos));
2026-04-07 20:01:06 +08:00
}
2026-04-11 10:09:40 +08:00
/**
* 将Cartesian3坐标转换为屏幕坐标
* @param pos 坐标
* @returns 偏移量
*/
convertScreenPosition(pos: Cartesian3): { x: number; y: number } {
const windowCoord = SceneTransforms.wgs84ToWindowCoordinates(
this.getViewer()!.scene,
pos
);
return { x: windowCoord.x, y: windowCoord.y };
2026-04-11 10:09:40 +08:00
}
2026-04-08 18:17:54 +08:00
// ===================== 私有方法 =====================
/**
2026-04-08 18:59:00 +08:00
* 检查管理器是否已初始化
* @param manager - 管理器实例
* @param managerName - 管理器名称(用于错误提示)
2026-04-08 18:17:54 +08:00
*/
#checkManager(manager: unknown, managerName: string): void {
2026-04-08 18:59:00 +08:00
if (!manager) {
throw new Error(`${managerName} 未初始化,请先调用 initCesiumViewer()`);
2026-04-08 18:17:54 +08:00
}
}
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
2026-04-08 18:59:00 +08:00
/**
* CesiumUtils 单例实例
*/
export const CesiumUtilsSingleton = new CesiumUtils();