Cesium实战系列文章总目录
:
传送门
1.背景
在实现正多边形动态扩散墙效果时,定义了正多边形扩散墙类WallRegularDiffuse
,在类内使用CallbackProperty
函数,动态更新墙体位置坐标和高度信息,目标成果如下图所示,但是发现没有任何效果,也没有报错
。
2.解决
(1)首先仔细查看了官方API
接口,确认没有调用错误。
CallbackProperty接口官方API介绍:传送门
(2)其次仔细检查了调用的函数和参数等,确认都已初始化,可以正常调用。应该和其他变量无关。
// 初始化
function _init() {
this._viewer.entities.add({
wall: {
position: new Cesium.CallbackProperty(function() {
let positions = [];
this._currentRadius += this._radius / 100.0;
this._currentHeight += this._height / 100.0;
if (this._currentRadius > this._radius || this._currentHeight < 0) {
this._currentRadius = 0;
this._currentHeight = 100.0;
}
positions = this._getPositions();
return positions;
}, false),
maximumHeights: new Array(6).fill(200),
minimunHeights: new Array(6).fill(0),
material: new Cesium.Color(1.0, 1.0, 0.0, 1.0),
}
})
}
(3)使用控制台console.log()
输出调试信息
在CallbackProperty函数内部
添加输出调试信息代码。
console.log("调试");
发现控制台没有打印
出任何信息,说明函数没有成功运行。
(4)更新代码
通过仔细观察这段代码和之前代码的不同,发现这里是在类内定义的函数,使用了this全局变量
。
然后我就把所有的代码都用函数function实现来替换之前的类class,然后就可以正常运行
了,这里猜想可能与this有关,有清楚具体原因的可以评论区留言交流。
修改后代码中调用CallbackProperty的那部分函数:
// 添加多边形
_viewer.entities.add({
wall: {
// callbackProperty回调函数,实时更新
positions: new Cesium.CallbackProperty(() => {
let positions = [];
_currentRadius += _radius * _speed / 1000.0;
_currentHeight -= _height * _speed / 1000.0;
// 判断扩散的实际半径和高度是否超出范围
if (_currentRadius > _radius || _currentHeight < 0) {
_currentRadius = _minRadius;
_currentHeight = _height;
}
positions = _getPositions(_center, _edge, _currentRadius, _currentHeight);
return positions;
}, false),
// 设置材质
material: new Cesium.WallDiffuseMaterialProperty({
color: new Cesium.Color(1.0, 1.0, 0.0, 1.0)
})
}
})