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!
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 common 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);