From ece181e5a39021545b815fc275dc00a179c299bc Mon Sep 17 00:00:00 2001 From: wzy-warehouse <18135009705@163.com> Date: Wed, 8 Apr 2026 22:20:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E5=8A=9F=E8=83=BD=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=BA=94=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 18 +++++------ src/component/map/Map.vue | 1 + src/types/cesium/GeoJsonOptions.ts | 1 + src/utils/cesium/CesiumUtils.ts | 46 ++++++++++++++++++++++++++-- src/utils/cesium/GeoJsonManager.ts | 36 ++++++++++++++++++---- src/utils/cesium/LayerManager.ts | 18 +++++++++++ src/utils/cesium/PrimitiveManager.ts | 18 +++++++++++ 7 files changed, 120 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index e338c28..a85642d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "preview": "vite preview" }, "dependencies": { - "vue": "^3.5.32", "@element-plus/icons-vue": "^2.3.2", "@types/spark-md5": "^3.0.5", "axios": "^1.12.2", @@ -18,26 +17,27 @@ "gm-crypto": "^0.1.12", "pinia": "^3.0.3", "spark-md5": "^3.0.2", - "vue-router": "^4.6.3", - "vite-plugin-cesium": "^1.2.22" + "vite-plugin-cesium": "^1.2.22", + "vue": "^3.5.32", + "vue-router": "^4.6.3" }, "devDependencies": { + "@tsconfig/node22": "^22.0.2", "@types/node": "^24.12.2", "@vitejs/plugin-vue": "^6.0.5", - "@vue/tsconfig": "^0.9.1", - "typescript": "~6.0.2", - "vite": "^8.0.4", - "vue-tsc": "^3.2.6", - "@tsconfig/node22": "^22.0.2", "@vue/eslint-config-prettier": "^10.2.0", "@vue/eslint-config-typescript": "^14.6.0", + "@vue/tsconfig": "^0.9.1", "eslint": "^9.37.0", "eslint-plugin-vue": "~10.5.0", "jiti": "^2.6.1", "npm-run-all2": "^8.0.4", "prettier": "3.6.2", + "typescript": "~6.0.2", "unplugin-auto-import": "^20.2.0", "unplugin-vue-components": "^30.0.0", - "vite-plugin-vue-devtools": "^8.0.3" + "vite": "^8.0.4", + "vite-plugin-vue-devtools": "^8.0.3", + "vue-tsc": "^3.2.6" } } diff --git a/src/component/map/Map.vue b/src/component/map/Map.vue index 7e66da9..f6e9236 100644 --- a/src/component/map/Map.vue +++ b/src/component/map/Map.vue @@ -39,6 +39,7 @@ onMounted(() => { const areaName = area.features[0].properties.name; return { showName: true, + default: true, labelStyle: { labelText: areaName, center: [area.features[0].properties.center[0], area.features[0].properties.center[1], 0], diff --git a/src/types/cesium/GeoJsonOptions.ts b/src/types/cesium/GeoJsonOptions.ts index 5dd7eb1..7834878 100644 --- a/src/types/cesium/GeoJsonOptions.ts +++ b/src/types/cesium/GeoJsonOptions.ts @@ -11,6 +11,7 @@ export type CustomizeGeoJsonDataSource = string | object; // 唯一配置项接口 export interface GeoJsonOptions { showName?: boolean; + default: boolean; labelStyle?: LabelConfig; polygonStyle?: { fill?: boolean; diff --git a/src/utils/cesium/CesiumUtils.ts b/src/utils/cesium/CesiumUtils.ts index 3364b29..0877d8f 100644 --- a/src/utils/cesium/CesiumUtils.ts +++ b/src/utils/cesium/CesiumUtils.ts @@ -106,6 +106,16 @@ export class CesiumUtils { this.#entityManager!.clearAllEntities(clearType) } + /** + * 获取所有实体ID + * @param clearType - 类型:'default'=默认实体,'custom'=自定义实体,'all'=所有实体(默认 'all') + * @returns 实体 ID 集合 + */ + getEntityIds(clearType: ClearType = 'all'): Set { + this.#checkManager(this.#entityManager, 'EntityManager') + return this.#entityManager!.getEntityIds(clearType) + } + // ===================== Primitive 管理 ===================== /** @@ -146,6 +156,16 @@ export class CesiumUtils { this.#primitiveManager!.clearAllPrimitives(clearType) } + /** + * 获取所有Primitive ID + * @param clearType - 类型:'default'=默认 Primitive,'custom'=自定义 Primitive,'all'=所有 Primitive(默认 'all') + * @returns Primitive ID 集合 + */ + getPrimitiveIds(clearType: ClearType = 'all'): Set { + this.#checkManager(this.#primitiveManager, 'PrimitiveManager') + return this.#primitiveManager!.getPrimitiveIds(clearType) + } + // ===================== 图层管理 ===================== /** @@ -196,6 +216,16 @@ export class CesiumUtils { this.#layerManager!.clearAllLayers(clearType) } + /** + * 获取所有图层 Key + * @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all') + * @returns 图层 Key 集合 + */ + getLayerKeys(clearType: ClearType = 'all'): Set { + this.#checkManager(this.#layerManager, 'LayerManager') + return this.#layerManager!.getLayerKeys(clearType) + } + // ===================== GeoJSON 图层管理 ===================== /** @@ -243,14 +273,14 @@ export class CesiumUtils { * @param isDefaults - 是否为默认图层数组 * @param options - 配置选项数组 */ - batchAddGeoJsonLayers( + async batchAddGeoJsonLayers( layerIds: string[], geojsonDatas: CustomizeGeoJsonDataSource[], isDefaults: boolean[], options?: GeoJsonOptions[] - ): void { + ): Promise { this.#checkManager(this.#geoJsonManager, 'GeoJsonManager') - this.#geoJsonManager!.batchAddGeoJsonLayers(layerIds, geojsonDatas, isDefaults, options) + await this.#geoJsonManager!.batchAddGeoJsonLayers(layerIds, geojsonDatas, isDefaults, options) } /** @@ -331,6 +361,16 @@ export class CesiumUtils { return this.#geoJsonManager!.getGeoJsonLayerVisibility(layerId) } + /** + * 获取所有 GeoJSON 图层 ID + * @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all') + * @returns GeoJSON 图层 ID 集合 + */ + getGeoJsonLayerIds(clearType: ClearType = 'all'): Set { + this.#checkManager(this.#geoJsonManager, 'GeoJsonManager') + return this.#geoJsonManager!.getGeoJsonLayerIds(clearType) + } + // ===================== 视角控制 ===================== /** diff --git a/src/utils/cesium/GeoJsonManager.ts b/src/utils/cesium/GeoJsonManager.ts index 1b51351..65fbb9c 100644 --- a/src/utils/cesium/GeoJsonManager.ts +++ b/src/utils/cesium/GeoJsonManager.ts @@ -32,6 +32,7 @@ export class GeoJsonManager { // 默认配置 static readonly DEFAULT_OPTIONS: Required = { showName: false, + default: false, labelStyle: { labelFont: '16px "微软雅黑"', labelColor: Color.RED, @@ -70,7 +71,7 @@ export class GeoJsonManager { * 添加 GeoJSON 图层 * @param layerId - 图层唯一标识 * @param geojsonData - GeoJSON 数据(路径、URL 或对象) - * @param isDefault - 是否为默认图层(默认 false) + * @param isDefault - 是否为默认图层(默认 false,优先级高于 options.default) * @param options - 配置选项(样式、标签等) * @returns Promise 数据源实例 */ @@ -82,6 +83,9 @@ export class GeoJsonManager { ): Promise { if (this.#exists(layerId)) throw new Error(`图层 ${layerId} 已存在`) const opt = this.#mergeOptions(options) + + // 优先使用 isDefault 参数,其次使用 options.default + const finalIsDefault = isDefault || opt.default // 加载并应用样式 const dataSource = await GeoJsonDataSource.load(geojsonData) @@ -89,7 +93,7 @@ export class GeoJsonManager { // 添加到地图 await this.#viewer.dataSources.add(dataSource) - isDefault + finalIsDefault ? this.#defaultGeoJsonMap.set(layerId, dataSource) : this.#customGeoJsonMap.set(layerId, dataSource) @@ -145,14 +149,16 @@ export class GeoJsonManager { * @param isDefaults - 是否为默认图层数组 * @param options - 配置选项数组 */ - batchAddGeoJsonLayers( + async batchAddGeoJsonLayers( layerIds: string[], geojsonDatas: CustomizeGeoJsonDataSource[], isDefaults: boolean[], options?: GeoJsonOptions[], - ): void { - layerIds.forEach((id, index) => - this.addGeoJsonLayer(id, geojsonDatas?.[index], isDefaults?.[index], options?.[index]), + ): Promise { + await Promise.all( + layerIds.map((id, index) => + this.addGeoJsonLayer(id, geojsonDatas?.[index], isDefaults?.[index], options?.[index]) + ) ) } @@ -255,6 +261,15 @@ export class GeoJsonManager { return ds ? ds.show : null } + /** + * 获取所有 GeoJSON 图层 ID + * @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all') + * @returns GeoJSON 图层 ID 集合 + */ + getGeoJsonLayerIds(clearType: ClearType = 'all'): Set { + return this.#getTargetIdsByType(clearType) + } + // ===================== 私有方法 ===================== /** 图层是否存在 */ @@ -384,4 +399,13 @@ export class GeoJsonManager { #convertPosition(pos: Cartesian3 | [number, number, number]): Cartesian3 { return Array.isArray(pos) ? Cartesian3.fromDegrees(pos[0], pos[1], pos[2] || 0) : pos } + + #getTargetIdsByType(clearType: ClearType): Set { + const targetIds = new Set() + if (clearType === 'default' || clearType === 'all') + this.#defaultGeoJsonMap.forEach((_, key) => targetIds.add(key)) + if (clearType === 'custom' || clearType === 'all') + this.#customGeoJsonMap.forEach((_, key) => targetIds.add(key)) + return targetIds + } } diff --git a/src/utils/cesium/LayerManager.ts b/src/utils/cesium/LayerManager.ts index 5b7dd7c..a697cf3 100644 --- a/src/utils/cesium/LayerManager.ts +++ b/src/utils/cesium/LayerManager.ts @@ -89,6 +89,15 @@ export class LayerManager { this.#clearMapsByType(clearType) } + /** + * 获取所有图层 Key + * @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all') + * @returns 图层 Key 集合 + */ + getLayerKeys(clearType: 'default' | 'custom' | 'all' = 'all'): Set { + return this.#getTargetIdsByType(clearType) + } + // ===================== 私有方法 ===================== #validateUniqueLayerKey(key: string): void { @@ -155,4 +164,13 @@ export class LayerManager { if (clearType === 'default' || clearType === 'all') this.#defaultLayerMap.clear() if (clearType === 'custom' || clearType === 'all') this.#customLayerMap.clear() } + + #getTargetIdsByType(clearType: 'default' | 'custom' | 'all'): Set { + const targetIds = new Set() + if (clearType === 'default' || clearType === 'all') + this.#defaultLayerMap.forEach((_, key) => targetIds.add(key)) + if (clearType === 'custom' || clearType === 'all') + this.#customLayerMap.forEach((_, key) => targetIds.add(key)) + return targetIds + } } diff --git a/src/utils/cesium/PrimitiveManager.ts b/src/utils/cesium/PrimitiveManager.ts index 4c72b90..e42beaa 100644 --- a/src/utils/cesium/PrimitiveManager.ts +++ b/src/utils/cesium/PrimitiveManager.ts @@ -83,6 +83,15 @@ export class PrimitiveManager { this.#clearMapsByType(clearType) } + /** + * 获取所有Primitive ID + * @param clearType - 类型:'default'=默认 Primitive,'custom'=自定义 Primitive,'all'=所有 Primitive(默认 'all') + * @returns Primitive ID 集合 + */ + getPrimitiveIds(clearType: 'default' | 'custom' | 'all' = 'all'): Set { + return this.#getTargetIdsByType(clearType) + } + // ===================== 私有方法 ===================== #groupPrimitivesByType(primitives: PrimitiveOptions[]) { @@ -266,6 +275,15 @@ export class PrimitiveManager { if (clearType === 'custom' || clearType === 'all') this.#customPrimitiveMap.clear() } + #getTargetIdsByType(clearType: 'default' | 'custom' | 'all'): Set { + const targetIds = new Set() + if (clearType === 'default' || clearType === 'all') + this.#defaultPrimitiveMap.forEach((_, key) => targetIds.add(key)) + if (clearType === 'custom' || clearType === 'all') + this.#customPrimitiveMap.forEach((_, key) => targetIds.add(key)) + return targetIds + } + #convertPosition(pos: Cartesian3 | [number, number, number]): Cartesian3 { return Array.isArray(pos) ? Cartesian3.fromDegrees(pos[0], pos[1], pos[2] || 0) : pos }