cesium实现点聚合 举个例子
时间: 2023-10-21 22:11:28 浏览: 107
假设有一组地理坐标点数据,如下所示:
| 纬度 | 经度 |
| ---- | ---- |
| 31.231 | 121.473 |
| 31.231 | 121.475 |
| 31.233 | 121.477 |
| 31.235 | 121.475 |
| 31.237 | 121.473 |
| 31.239 | 121.471 |
| 31.241 | 121.469 |
| 31.243 | 121.471 |
| 31.245 | 121.473 |
| 31.247 | 121.475 |
我们可以使用Cesium实现点聚合,将附近的点合并成一个聚合点。具体实现步骤如下:
1. 创建Cesium Viewer对象:
```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
```
2. 将点数据转换成Cesium的Entity对象,并添加到Viewer中:
```javascript
var entities = [];
var positions = [
[31.231, 121.473],
[31.231, 121.475],
[31.233, 121.477],
[31.235, 121.475],
[31.237, 121.473],
[31.239, 121.471],
[31.241, 121.469],
[31.243, 121.471],
[31.245, 121.473],
[31.247, 121.475]
];
for (var i = 0; i < positions.length; i++) {
var position = Cesium.Cartesian3.fromDegrees(positions[i][1], positions[i][0]);
var entity = new Cesium.Entity({
position: position,
point: {
pixelSize: 10,
color: Cesium.Color.RED
}
});
entities.push(entity);
}
viewer.entities.add(entities);
```
3. 使用Cesium的Cluster实现点聚合:
```javascript
var pixelRange = 50;
var minimumClusterSize = 3;
var cluster = new Cesium.Cluster({
pixelRange: pixelRange,
minimumClusterSize: minimumClusterSize,
enabled: true
});
viewer.dataSources.add(cluster);
cluster.addEntities(entities);
```
以上代码中,我们使用了Cesium的Cluster对象,设置了聚合点的像素范围和最小聚合点数量。最后将Entity添加到Cluster中即可实现点聚合。
完整代码如下:
```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
var entities = [];
var positions = [
[31.231, 121.473],
[31.231, 121.475],
[31.233, 121.477],
[31.235, 121.475],
[31.237, 121.473],
[31.239, 121.471],
[31.241, 121.469],
[31.243, 121.471],
[31.245, 121.473],
[31.247, 121.475]
];
for (var i = 0; i < positions.length; i++) {
var position = Cesium.Cartesian3.fromDegrees(positions[i][1], positions[i][0]);
var entity = new Cesium.Entity({
position: position,
point: {
pixelSize: 10,
color: Cesium.Color.RED
}
});
entities.push(entity);
}
var pixelRange = 50;
var minimumClusterSize = 3;
var cluster = new Cesium.Cluster({
pixelRange: pixelRange,
minimumClusterSize: minimumClusterSize,
enabled: true
});
viewer.dataSources.add(cluster);
cluster.addEntities(entities);
```
阅读全文