最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cesiumjs: How to iterate data from GeoJsonDataSource - Stack Overflow

programmeradmin3浏览0评论

could anyone tell me how to get the position data from GeoJsonDataSource? Here is what I am doing:

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);
// []

Thank you very much for your help!

could anyone tell me how to get the position data from GeoJsonDataSource? Here is what I am doing:

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);
// []

Thank you very much for your help!

Share Improve this question edited Oct 16, 2014 at 13:52 Robert asked Oct 16, 2014 at 3:32 RobertRobert 2,2497 gold badges32 silver badges39 bronze badges 2
  • Several questions that might be typo: l.2 x->entity1? l.3 assocArray->array1? l.6 x->entity1? – dgiugg Commented Oct 16, 2014 at 9:43
  • @dgiugg Thanks for replying. Sorry it was a typo in this question, x should be entity1. In my project on the server, when I check the members of entity1.entities in line 2, or assocArray in line 6, the re are 500 data instances in them. – Robert Commented Oct 16, 2014 at 13:58
Add a ment  | 

1 Answer 1

Reset to default 8

GeoJsonDataSource.fromUrl returns a new instance that is still in the process of loading data (the isLoading property will be true). You can't use the data in the data source until the loadingEvent event is fired. In cases like this, it's easier to create the new instance yourself and use loadUrl instead. This is still an asynchronous operation; but it returns a promise that resolved when the data is ready. See Cesium's GeoJSON Sandcastle demo for an example of doing this. This is a mon pattern, not just in Cesium, but JavaScript in general. You can read more about the promise system used by Cesium here.

Here's a small snippet of code that shows you how to iterate.

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);
发布评论

评论列表(0)

  1. 暂无评论