let areaDataSource = []; let isEffectActive = true; // 标记当前效果是否激活 const minHeight = 7000; // 定义相机高度阈值(单位:米) export function loadAreaPolygonAll(url, isPolluted = false) { let color = new Cesium.Color(1.0, 0.0, 0.0, 0.1); if (isPolluted) { color = new Cesium.Color(1.0, 0.0, 0.0, 0.1); } // 使用 GeoJsonDataSource 加载 GeoJSON 数据 const dataSourcePromise = Cesium.GeoJsonDataSource.load(url, { clampToGround: true, }); // 获取 GeoJSON 中的 Polygon feature return dataSourcePromise.then(function (dataSource) { viewer.dataSources.add(dataSource); console.log("GeoJSON data loaded:", dataSource); areaDataSource.push(dataSource); const polygonEntity = dataSource.entities.values; console.log("Entities in dataSource:", polygonEntity); // 初始化效果 applyPolygonEffects(polygonEntity, color); // 监听相机移动事件 viewer.camera.moveEnd.addEventListener(updatePolygonEffects); console.log("Camera moveEnd event listener added."); }); } function applyPolygonEffects(entities, color) { console.log("Applying polygon effects..."); entities.forEach(entity => { entity.polygon.heightReference = Cesium.HeightReference.NONE; entity.polygon.material = new Cesium.ColorMaterialProperty(color); // 设置填充颜色 entity.polygon.outline = true; // 启用描边 entity.polygon.outlineColor = Cesium.Color.RED; // 设置描边颜色 entity.polygon.Height = 10.0; // 确保不重复添加实体 if (!viewer.entities.getById(entity.id)) { viewer.entities.add(entity); } console.log("Effect applied to entity:", entity.id); }); } function removePolygonEffects(entities) { console.log("Removing polygon effects..."); entities.forEach(entity => { if (entity.polygon) { // 设置透明填充颜色 entity.polygon.material = new Cesium.ColorMaterialProperty( new Cesium.Color(1.0, 1.0, 1.0, 0.0) // 完全透明 ); // 清除描边 entity.polygon.outline = false; entity.polygon.outlineColor = undefined; console.log("Effect removed from entity:", entity.id); } }); } function updatePolygonEffects() { const cameraHeight = viewer.camera.positionCartographic.height; console.log("Camera height updated:", cameraHeight, "meters"); if (cameraHeight < minHeight && isEffectActive) { // 如果相机高度低于阈值且效果是激活状态,则移除效果 console.log("Camera height below threshold. Removing effects..."); areaDataSource.forEach(dataSource => { removePolygonEffects(dataSource.entities.values); }); isEffectActive = false; console.log("Effects removed. Current effect state:", isEffectActive); } else if (cameraHeight >= minHeight && !isEffectActive) { // 如果相机高度高于阈值且效果未激活,则重新应用效果 console.log("Camera height above threshold. Reapplying effects..."); areaDataSource.forEach(dataSource => { applyPolygonEffects(dataSource.entities.values, new Cesium.Color(1.0, 0.0, 0.0, 0.1)); }); isEffectActive = true; console.log("Effects reapplied. Current effect state:", isEffectActive); } }