guonan
2025-04-14 9e860a560c5a4b81abe2042b8d8698e253730502
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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);
    }
}