3

谁能告诉我如何从 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);
// []

非常感谢您的帮助!

4

1 回答 1

8

GeoJsonDataSource.fromUrl返回一个仍在加载数据过程中的新实例(该isLoading属性将为 true)。loadingEvent在触发事件之前,您不能使用数据源中的数据。在这种情况下,自己创建新实例并改为使用会更容易loadUrl。这仍然是一个异步操作;但它返回一个在数据准备好时解决的承诺。有关执行此操作的示例,请参阅 Cesium 的GeoJSON Sandcastle 演示。这是一种常见的模式,不仅在 Cesium 中,在整个 JavaScript 中也是如此。您可以在此处阅读有关 Cesium 使用的承诺系统的更多信息。

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

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);
于 2014-10-16T14:25:41.227 回答