Cesium从外部数据流动态更新实体

时间:2016-11-01 20:55:55

标签: cesium

我是Cesium的一个总菜鸟,请原谅我任何愚蠢。我正在尝试编写一个应用程序,将位置和方向数据流式传输到Cesium,在Cesium中实时绘制,并显示一个显示其位置的路径。我遇到的问题是实体存在视觉上的口吃,几乎可以肯定是由于entity.position属性的更新速度比绘制调用可以执行的速度快。我对路径折线有同样的问题,但找到了一个代码片段,为我修复了它:

var pathtrace = new Cesium.PolylineCollection();

primitives = viewer.scene.primitives;

var objpath = pathtrace.add({
name : 'Path',
polyline : {
        positions : Cesium.Cartesian3.fromDegreesArrayHeights([0, 0, 0,
                                                            0, 0, 0])
    }
});
primitives.add(pathtrace);

...Inside loop...

data = JSON.parse(result.data);
objpos = data.concat(objpos);
objpath.positions = Cesium.Cartesian3.fromDegreesArrayHeights(objpos);

但是,我无法找到与PolylineCollection具有相同功能的任何内容,以优化动态更新实体。现在我正在使用:

var vehicle = viewer.entities.add({
    name : "Vehicle",
    position : Cesium.Cartesian3.fromDegrees(0, 0),
    orientation : orientation,
    model : {
        url : url,
        minimumPixelSize : 50
    }
});

...Inside loop...

vehicle.position = Cesium.Cartesian3.fromDegrees(data[0], data[1], data[2]); 

...这导致实体在移动时来回跳跃。有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用callbackProperty。像这样:

// init somewhere
var vehiclesPositions = {}; 

// when you parse the data for each vhicle
... init loop ...
let vhicle = vhicles[i]; // use let to make sure vhicle is always the same for this loop scope
vehiclesPositions[vhicle.id] = Cesium.Cartesian3.fromDegrees(vhicle.longitude, vhicle.latitude);

var vehicle = viewer.entities.add({
    name : "Vehicle",
    position : new Cesium.CallbackProperty(function() { return vehiclesPositions[vhicle.id]; }, // I can't cover all of the issues that might arise with the scope here. See note for the let vhicle line
    orientation : orientation,
    model : {
        url : url,
        minimumPixelSize : 50
    }
});

... end init loop ...

然后你做了更新循环:

...在更新循环中......

// I assume you get the right ID somehow
vehiclesPositions[vhicle.id] = Cesium.Cartesian3.fromDegrees(data[0], data[1], data[2]); 

...结束更新循环...

然后你应该停止看口吃。