Cesiumjs:如何从GeoJsonDataSource迭代数据

时间:2014-10-16 03:32:25

标签: javascript cesium

谁能告诉我如何从GeoJsonDataSource获取位置数据?这就是我在做的事情:

entity1 = Cesium.GeoJsonDataSource.fromUrl('../../SampleData/markersdata.geojson');
var array1 = entity1.entities.entities;        //According to document, this should an array of entity instances, but it only returns an empty array.
console.log(array1);
// []
//If I do this:
var assocArray = entity1.entities._entities;       //This returns an associative array
var markersArr = assocArray.values;          //I expect this returns an array of values, but it still returns empty array.
console.log(markersArr);
// []

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:6)

GeoJsonDataSource.fromUrl返回仍在加载数据的新实例(isLoading属性为true)。在loadingEvent事件被触发之前,您无法使用数据源中的数据。在这种情况下,您可以更轻松地自行创建新实例并使用loadUrl代替。这仍然是异步操作;但它返回一个在数据准备好时解决的承诺。有关此操作的示例,请参阅Cesium的GeoJSON Sandcastle demo。这是一种常见的模式,不仅仅是在Cesium中,而是一般的JavaScript。您可以阅读有关Cesium here使用的承诺系统的更多信息。

这是一小段代码,向您展示如何进行迭代。

var dataSource = new Cesium.GeoJsonDataSource();
dataSource.loadUrl('../../SampleData/ne_10m_us_states.topojson').then(function() {
    var entities = dataSource.entities.entities;
    for (var i = 0; i < entities.length; i++) {
        var entity = entities[i];
        ...
    }
});
viewer.dataSources.add(dataSource);