cesium解决使用CallbackProperty无效果也不报错问题

文章目录


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)
         })
     }
 })

参考:
参考1:传送门
参考2:传送门

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

右弦GISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值