在 Cesium 中,使用 Cesium.PointPrimitiveCollection
来加载和管理点时,可以通过 removeAll()
方法清除所有添加的点。以下是如何使用 PointPrimitiveCollection
的示例:
1. 添加点
首先,你需要创建一个 PointPrimitiveCollection
并将其添加到场景中:
// 创建 PointPrimitiveCollection
const pointCollection = new Cesium.PointPrimitiveCollection();
viewer.scene.primitives.add(pointCollection);
// 添加点的示例
const addPoint = (longitude, latitude, altitude) => {
pointCollection.add({
position: Cesium.Cartesian3.fromDegrees(longitude, latitude, altitude),
color: Cesium.Color.RED,
pixelSize: 10, // 点的大小
});
};
// 示例:添加几个点
addPoint(116.38, 39.9, 100); // 北京
addPoint(121.47, 31.23, 100); // 上海
2. 清除所有点
要清除 PointPrimitiveCollection
中的所有点,可以使用 removeAll()
方法:
// 清除所有通过 PointPrimitiveCollection 添加的点
pointCollection.removeAll();
3. 清除特定的点
如果你想清除特定的点,可以在添加时保存对该点的引用:
// 添加特定点并保存引用
const specificPoint = pointCollection.add({
position: Cesium.Cartesian3.fromDegrees(116.38, 39.9, 100),
color: Cesium.Color.RED,
pixelSize: 10,
});
// 清除特定的点
pointCollection.remove(specificPoint);
总结
- 使用
PointPrimitiveCollection
来管理点时,可以通过removeAll()
清除所有点。 - 若要清除特定点,请在添加时保存对该点的引用,使用
remove()
方法进行清除。