Add EntityCollection to viewer

Hi

I am sure this is a trivial question.

I am adding a number of polylines to an entity collection. I am then having problems adding the collection to the viewer.

var myEntityCollection = new Cesium.EntityCollection;

//In a loop

myEntityCollection.add({

polyline: {

positions: Cesium.Cartesian3.fromDegreesArrayHeights(rte),

width: 2,

material: new Cesium.Color(color[1], color[2], color[3]),

followSurface: true

}

});

console.log(myEntityCollection.values); //I can see the entities have been added

viewer.entities.add(myEntityCollection); //The entities dont show on the globe

Thanks

I don’t see any usage examples here

http://cesiumjs.org/Cesium/Build/Documentation/EntityCollection.html

I created a test based off the polyline demo, just change the test boolean to test it

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var myEntityCollection = new Cesium.EntityCollection();
var test=false;
if(test){var prefix=myEntityCollection;}
else{var prefix=viewer.entities;}

var redLine = prefix.add({
name : ‘Red line on the surface’,
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray([-75, 35,
-125, 35]),
width : 5,
material : Cesium.Color.RED
}
});

var glowingLine = prefix.add({
name : ‘Glowing blue line on the surface’,
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray([-75, 37,
-125, 37]),
width : 10,
material : new Cesium.PolylineGlowMaterialProperty({
glowPower : 0.2,
color : Cesium.Color.BLUE
})
}
});

if(test){viewer.entities.add(myEntityCollection);}

viewer.zoomTo(viewer.entities);

``

Hi,

Thanks for your reply. I tried running the above code and it never adds the entities to the map when using the entity collection.

I am sure I am missing something basic, so for now will continue to use the viewer.entities.add for every entity I wish to add.

If anyone can provide any points as to why viewer.entities.add(myEntityCollection); doesnt seem to add the collection to the globe, that would be great.

Thanks

It would be nice if the reference docs would link to some SandCastle apps demonstrating the features.

https://github.com/NautSoft/angular-cesiumjs/blob/master/angular-cesiumjs.js

While probably not 100% what you’re looking for check out this module for Angular. I do some Collection management within it.

Thanks to Matt’s other post I found some info here

Scroll down to where it says “EntityCollection is an associative array that makes managing and monitoring a group of entities easy”

There’s a code snippet there where you copy/paste into

Add this after the for loop after copy and pasting

console.log(msg);

Maybe the ref docs could link to the Visualizing-Spatial-Data page as it provides a good overview explanation.

Thanks again. I did read this tutorial a few times before posting on the forum. I was still left a little confused.

Is this the code snippet you refer to?

function onChanged(collection, added, removed, changed){
  var msg = 'Added ids';
  for(var i = 0; i < added.length; i++) {
    msg += '\n' + added[i].id;
  }
}
viewer.entities.collectionChanged.addEventListener(onChanged);

I added this in sandbox but could not get it to work. I also was unsure how this would work with the myEnityCollection example I posted earlier.

Thanks

Ya that code snippet builds up a msg string but doesn’t seem to output it to the console, that’s why I added console.log(msg).

I don’t think viewer.entities.add(myEntityCollection); is a valid statement, even though it doesn’t throw any errors. I believe you need to use viewer.entities.add to add each entity to the scene. I think entityCollection is intended to keep track of a group of entities, but not to add them as a whole group to the scene. I believe entityCollection is an array of unique ids that refer to entity objects, rather than an array of entities objects themselves.

Looking at Michaels example it seems that for primitives you can add collections, but I could be wrong.

Gah… so the problem is actually simple… viewer.entities is a “getter” only property. You cannot assign a new collection on top of it.

So myEntityCollection = viewer.entities; then you can add all your entities to that collection.

If you need to have separate collections then you will probably need to do something similar to what I did.

well, just for those messing with the same s**t and wandered here like I did — look at CustomDataSource section at cesium docs. have no idea, why such a logically desired feature is not described well while it's relatively convoluted implemented, but God bless it nevertheless exists

Did you ever find a solution?

Expanding :slight_smile: Alex K’s answer:

myDataSource = new Cesium.CustomDataSource(“my data”)

myDataSource.entities.add()
// repeat as needed

viewer.dataSources.add(myDataSource)

``

so you don’t create an EntityCollection, you create a CustomDataSource which comes with an EntityCollection.