更新 cesium polylinegeometry position
时间: 2023-10-06 13:05:28 浏览: 209
要更新Cesium PolylineGeometry的位置,您可以使用以下步骤:
1. 获取现有的PolylineGeometry对象:
```javascript
var polylineGeometry = polyline.geometry;
```
2. 获取现有的位置数组:
```javascript
var positions = polylineGeometry.positions;
```
3. 更新位置数组中的值:
```javascript
positions[0] = Cesium.Cartesian3.fromDegrees(lon1, lat1, height1);
positions[1] = Cesium.Cartesian3.fromDegrees(lon2, lat2, height2);
// 更改其他位置...
```
4. 将更新后的位置数组设置回PolylineGeometry对象:
```javascript
polylineGeometry.positions = positions;
```
5. 更新PolylineGeometry对象的外观:
```javascript
polyline.appearance = new Cesium.PolylineMaterialAppearance({
material: Cesium.Material.fromType('Color', {
color: Cesium.Color.YELLOW
})
});
```
完整示例代码如下:
```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
// 添加Polyline
var polyline = viewer.entities.add({
name: 'Polyline',
polyline: {
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
-75, 35, 0,
-125, 35, 0
]),
width: 10,
material: Cesium.Color.YELLOW
}
});
// 获取现有的PolylineGeometry对象
var polylineGeometry = polyline.geometry;
// 获取现有位置数组
var positions = polylineGeometry.positions;
// 更新位置数组中的值
positions[0] = Cesium.Cartesian3.fromDegrees(-75, 40, 0);
positions[1] = Cesium.Cartesian3.fromDegrees(-125, 40, 0);
// 将更新后的位置数组设置回PolylineGeometry对象
polylineGeometry.positions = positions;
// 更新PolylineGeometry对象的外观
polyline.appearance = new Cesium.PolylineMaterialAppearance({
material: Cesium.Material.fromType('Color', {
color: Cesium.Color.RED
})
});
```
阅读全文