Cesium 中的 ConstantProperty 表示一个常量属性,即该属性始终保持不变。
ConstantProperty 支持以下类型:
使用 ConstantProperty 可以让代码更简洁易懂,并且可以更方便地进行代码重构和调试。
以 Cartesian3 为例:
const position = new Cesium.ConstantProperty(Cesium.Cartesian3.fromDegrees(120.0, 30.0, 100.0));
ConstantProperty 提供了以下方法:
getValue(time, result)获取 ConstantProperty 的值。
time:JulianDate 类型,用于指定时间。如果为 undefined 或 null,则返回当前属性的值。result:可选的返回值参数。返回类型为 ConstantProperty 支持的数据类型之一。如果提供了 result,则将返回值写入该参数,否则返回新的值。
equals(other)比较两个 ConstantProperty 是否相等。
other:ConstantProperty 类型返回值为 true 如果两个 ConstantProperty 值相等,否则返回 false。
const color = new Cesium.ConstantProperty(Cesium.Color.RED);
const point = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(120.0, 30.0),
point: {
pixelSize: 10,
color: color,
}
});
color.setValue(Cesium.Color.BLUE);
// 等价于
point.point.color.setValue(Cesium.Color.BLUE);