forked from drone/command-center-dashboard

罗广辉
2025-03-29 f1e4e5843ff9ae37ccecd0ea391911f67de0e50d
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as Cesium from 'cesium';
import data2 from '@/assets/images/home/homeRight/data2.png';
import data1 from '@/assets/images/home/homeRight/data1.png';
import jiangxishi from '@/assets/geojson/jiangxishi.json';
import jiangxi from '@/assets/geojson/jiangxi.json';
import zg from '@/assets/geojson/zg.json';
import MapPopUpBox from '@/views/Home/MapPopUpBox.vue';
import { render } from 'vue';
 
/**
 * 机巢聚合功能
 */
export const useAggregation = () => {
  const scalingJudgment = [
    { name: '县', value: [0, 48651], gJson: null },
    { name: '市', value: [48651, 314863], gJson: jiangxishi },
    { name: '省', value: [314863, 1169651], gJson: jiangxi },
    { name: '国', value: [1169651, 37962800], gJson: zg },
  ];
  let viewer = null;
  const active = ref('');
  let handler = null;
  let labelDiv = null;
  let positionC3 = null;
  const init = () => {
    determineScaling();
    viewer.camera.moveEnd.addEventListener(() => {
      determineScaling();
    });
  };
 
  // 确定缩放比例
  const determineScaling = () => {
    let height = viewer.camera.positionCartographic.height;
    // 根据高度展示对应的 gJson
    for (let item of scalingJudgment) {
      if (height > item.value[0] && height <= item.value[1]) {
        if (active.value !== item.name) {
          removeEntities();
          removeLabel();
          active.value = item.name;
          item.gJson ? aggregation(item) : splashed();
        }
        break;
      }
    }
  };
 
  //散点机巢
  function splashed() {
    for (let i = 0; i < 50; i++) {
      viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(
          115.89 + Math.random() * 0.2,
          28.68 + Math.random() * 0.2
        ),
        label: {
          // 随机整数
          text: Math.floor(Math.random() * 100) + '号机巢',
          font: '14pt monospace',
          fillColor: Cesium.Color.WHITE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -9),
        },
        billboard: {
          image: new Cesium.ConstantProperty(data1),
          width: 24,
          height: 24,
        },
      });
    }
  }
 
  // 聚合机巢
  const aggregation = item => {
    const featuresList = item.gJson.features.map(item => ({
      name: item.properties.name,
      position: item.properties.center,
    }));
    // 遍历特征并添加实体
    featuresList.forEach(feature => {
      if (!feature.position) return;
      const position = Cesium.Cartesian3.fromDegrees(feature.position[0], feature.position[1]);
      viewer.entities.add({
        id: feature.id,
        position: position,
        label: {
          // 随机整数
          text: feature.name + Math.floor(Math.random() * 100),
          font: '14pt monospace',
          fillColor: Cesium.Color.WHITE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -9),
        },
        billboard: {
          image: new Cesium.ConstantProperty(data2),
          width: 24,
          height: 24,
        },
        properties: {
          id: feature.id,
          customData: {
            name: feature.name,
          },
        },
      });
    });
    // 加载新的 GeoJSON 数据
    Cesium.GeoJsonDataSource.load(item.gJson).then(dataSource => {
      viewer.dataSources.add(dataSource);
      item.dataSource = dataSource; // 保存数据源以便后续删除
      // 获取数据源中的实体
      const entities = dataSource.entities.values;
      entities.forEach(entity => {
        entity.polygon.material = new Cesium.ColorMaterialProperty(
          Cesium.Color.YELLOW.withAlpha(0) // 透明填充
        );
        entity.polygon.outline = new Cesium.ConstantProperty(true); // 显示边框
        entity.polygon.outlineColor = new Cesium.ConstantProperty(Cesium.Color.YELLOW); // 黑色边框
      });
    });
  };
 
  const getLabelDom = () => {
    const vNode = h(MapPopUpBox, { data: '参数',removeLabel });
    const tooltipContainer = document.createElement('div');
    tooltipContainer.style.position = 'absolute';
    tooltipContainer.style.transform = 'translateY(-50%)';
    tooltipContainer.style.pointerEvents = 'none';
    document.querySelector('.page-index').append(tooltipContainer);
    render(vNode, tooltipContainer);
    return tooltipContainer;
  };
 
  const labelBox = () => {
    if (!labelDiv) {
      labelDiv = getLabelDom();
    }
    const screenPosition = viewer.scene.cartesianToCanvasCoordinates(positionC3);
    if (screenPosition) {
      labelDiv.style.left = `${screenPosition.x}px`;
      labelDiv.style.top = `${screenPosition.y}px`;
    }
  };
 
  const clickFun = click => {
    const pickedObject = viewer.scene.pick(click.position);
    if (Cesium.defined(pickedObject) && pickedObject.id) {
      const entity = pickedObject.id;
      positionC3 = entity.position._value;
      viewer.scene.postRender.addEventListener(labelBox);
    }
  };
 
  const removeEntities = () => {
    viewer.dataSources.removeAll(true);
    viewer.entities.removeAll();
  };
  const removeLabel = () => {
    viewer.scene.postRender.removeEventListener(labelBox);
    if (labelDiv && labelDiv.parentNode) {
      labelDiv.parentNode.removeChild(labelDiv);
      labelDiv = null;
    }
  };
 
  onUnmounted(() => {
    removeEntities();
    removeLabel();
    handler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  });
 
  onMounted(() => {
    nextTick(() => {
      viewer = window.$viewer;
      handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
      handler.setInputAction(clickFun, Cesium.ScreenSpaceEventType.LEFT_CLICK);
      init();
    });
  });
};