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 ee48430..2b38d38 100644
--- a/src/components/h-cesium-viewer/h-cesium-viewer-class.ts
+++ b/src/components/h-cesium-viewer/h-cesium-viewer-class.ts
@@ -33,7 +33,7 @@ export class HCesiumViewerCls {
*/
addGroundStation(options: GroundStationOptions): Cesium.Entity | null {
if (!this.viewer) {
- console.error('视图未初始化。无法添加地面站。'); // 使用 console.error 替代抛出错误,以便在 index.vue 中处理
+ console.error('视图未初始化。无法添加地面站。');
return null;
}
@@ -101,13 +101,8 @@ export class HCesiumViewerCls {
}
}
- // 移除 highlightStation 和 unhighlightStation 方法
-
clearAllGroundStations() {
- // ... (清理逻辑不变)
if (!this.viewer) return;
- // 优化:直接遍历 Map 清理,避免重复查找
- // 使用 for...of 循环替代 forEach
for (const entity of this.currentStationEntities.values()) {
this.viewer?.entities.remove(entity);
}
@@ -115,7 +110,6 @@ export class HCesiumViewerCls {
}
destroy() {
- // ... (销毁逻辑不变)
if (this.viewer) {
this.clearAllGroundStations();
this.viewer.destroy();
diff --git a/src/components/h-cesium-viewer/index.vue b/src/components/h-cesium-viewer/index.vue
index 7527358..59fff2d 100644
--- a/src/components/h-cesium-viewer/index.vue
+++ b/src/components/h-cesium-viewer/index.vue
@@ -1,62 +1,28 @@
@@ -70,10 +36,4 @@ onBeforeUnmount(() => {
min-height: 300px;
background-color: #000;
}
-:deep(.cesium-widget),
-:deep(.cesium-widget canvas) {
- width: 100%;
- height: 100%;
- touch-action: none;
-}
diff --git a/src/components/h-cesium-viewer/types.ts b/src/components/h-cesium-viewer/types.ts
new file mode 100644
index 0000000..73831fe
--- /dev/null
+++ b/src/components/h-cesium-viewer/types.ts
@@ -0,0 +1,12 @@
+import type { GroundStationOptions } from './h-cesium-viewer-class';
+
+/**
+ * 地面站状态接口
+ * 包含地面站列表和当前选中的地面站 ID 列表
+ */
+export interface GroundStationState {
+ /** 地面站配置数组 */
+ groundStations: GroundStationOptions[];
+ /** 选中的地面站 ID 数组 */
+ selectedIds: string[];
+}
diff --git a/src/components/h-cesium-viewer/useHCesiumViewerCls.ts b/src/components/h-cesium-viewer/useHCesiumViewerCls.ts
new file mode 100644
index 0000000..16a4e90
--- /dev/null
+++ b/src/components/h-cesium-viewer/useHCesiumViewerCls.ts
@@ -0,0 +1,24 @@
+// src/utils/useHCesiumViewerCls.ts
+import { HCesiumViewerCls } from '@/components/h-cesium-viewer/h-cesium-viewer-class';
+
+/**
+ * 管理 HCesiumViewerCls 实例的生命周期。
+ * @param containerId - Cesium Viewer 容器的 DOM ID。
+ * @returns 返回 HCesiumViewerCls 实例。
+ */
+export function useHCesiumViewerCls(containerId: string) {
+ const hCesiumViewerInst = new HCesiumViewerCls();
+ if ($__DEV__) Object.assign(globalThis, { hCesiumViewerInst }); // 仅在开发模式下暴露到全局
+
+ onMounted(() => {
+ hCesiumViewerInst.initCesiumViewer(containerId);
+ });
+
+ onBeforeUnmount(() => {
+ hCesiumViewerInst.destroy();
+ });
+
+ return {
+ hCesiumViewerInst,
+ };
+}
diff --git a/src/components/h-cesium-viewer/useHCesiumViewerClsGroundStation.ts b/src/components/h-cesium-viewer/useHCesiumViewerClsGroundStation.ts
new file mode 100644
index 0000000..e9a2468
--- /dev/null
+++ b/src/components/h-cesium-viewer/useHCesiumViewerClsGroundStation.ts
@@ -0,0 +1,65 @@
+// src/utils/useHCesiumViewerClsGroundStation.ts
+import type { GroundStationOptions, HCesiumViewerCls } from '@/components/h-cesium-viewer/h-cesium-viewer-class';
+import type { MaybeRefOrGetter } from 'vue';
+
+/**
+ * 管理 Cesium Viewer 中的地面站实体,根据选中的 ID 列表进行同步。
+ * @param hCesiumViewerInst - HCesiumViewerCls 实例或其 getter。
+ * @param groundStationList - 包含所有可用地面站选项的数组或 getter。
+ * @param selectedStationIds - 包含当前选中地面站 ID 的数组或 getter。
+ */
+export function useHCesiumViewerClsGroundStation(
+ hCesiumViewerInst: MaybeRefOrGetter,
+ groundStationList: MaybeRefOrGetter | undefined>,
+ selectedStationIds: MaybeRefOrGetter,
+) {
+ // 创建一个从 ID 到站点选项的映射,方便查找
+ const stationMap = computed(() => {
+ const map = new Map();
+ const list = toValue(groundStationList) ?? [];
+ for (const station of list) {
+ map.set(station.id, station);
+ }
+ return map;
+ });
+
+ // watchEffect: 同步 selectedStationIds 到 Cesium 实体
+ watchEffect(() => {
+ const viewerInstance = toValue(hCesiumViewerInst);
+ if (!viewerInstance) {
+ // 如果 viewer 实例尚未初始化或已销毁,则不执行任何操作
+ return;
+ }
+
+ const selectedIds = toValue(selectedStationIds) ?? [];
+ const selectedIdsSet = new Set(selectedIds); // 需要显示的站点 ID
+ const currentEntityIds = new Set(viewerInstance.currentStationEntities.keys()); // 当前已显示的站点 ID
+
+ // 1. 移除不再选中的站点
+ for (const entityId of currentEntityIds) {
+ if (!selectedIdsSet.has(entityId)) {
+ // 如果当前显示的实体不在新的选中列表里,则移除
+ viewerInstance.removeGroundStationById(entityId);
+ }
+ }
+
+ // 2. 添加新增的选中站点
+ for (const selectedId of selectedIdsSet) {
+ if (!currentEntityIds.has(selectedId)) {
+ // 如果选中的 ID 对应的实体当前未显示,则添加
+ const stationToAdd = stationMap.value.get(selectedId); // 从映射中查找站点信息
+ if (stationToAdd) {
+ viewerInstance.addGroundStation(stationToAdd);
+ } else {
+ // 如果在 groundStationList 中找不到对应的站点信息,发出警告
+ consola.warn(`无法找到 ID 为 "${selectedId}" 的站点信息,无法添加到地图。`);
+ }
+ }
+ }
+ });
+
+ // 返回 stationMap 可能在某些场景下有用,例如调试或扩展
+ return {
+ stationMap, // 可以考虑是否真的需要返回这个
+ };
+}
diff --git a/src/pages/cesium-viewer.page.vue b/src/pages/cesium-viewer.page.vue
index 78d8ac9..d9f8583 100644
--- a/src/pages/cesium-viewer.page.vue
+++ b/src/pages/cesium-viewer.page.vue
@@ -4,21 +4,23 @@ meta:
@@ -63,20 +65,24 @@ const clearAllStations = () => {
添加随机站点
-
移除最后一个
-
清空所有
-
当前站点数: {{ groundStations.length }}
-
| 选中站点数: {{ selectedIds.length }}
+
移除最后一个
+
清空所有
+
当前站点数: {{ groundStationState.groundStations.length }}
+
| 选中站点数: {{ groundStationState.selectedIds.length }}
-