How create a EntityCollection and remove

Hi,

I have a problem with adding Entity to the EntityCollection. I want to add them to the collection to later delete the whole collection with a single button.

var entities = viewer.entities;

var entity = new Cesium.Entity();
var collection = new Cesium.EntityCollection();

function createEntity(name, x, y, z, description) {

entity = entities.add({

id : name,

position : Cesium.Cartesian3.fromDegrees(x,y,z),

name : name,

description : ‘description’,

billboard : {

image : ‘picture/logou/’+nazwa+’.png’,

sizeInMeters : true

}

});

collection.add(entity);

viewer.zoomTo(entity, new Cesium.HeadingPitchRange(0, 0, 1000));

}

function clear(){

collection.removeAll();

};

``

I am using Cesium 1.45.

I think you are going to need a custom data source to hold your EntityCollection. See the Sandcastle “Custom DataSource” example under DataSources in the Gallery for inspiration.

Scott

I have reviewed this example and I do not know how to use it in my case ;/

W dniu poniedziałek, 29 października 2018 13:34:04 UTC+1 użytkownik Scott Reynolds napisał:

I think an easier way, if all you care about is keeping track of them and removing them later, is simply to add them to an array, and then loop over that array and remove all its items.

Here’s a Sandcastle example that will remove all the entities after a few seconds.

Let me know if that helps!

Thank you very much for the hint of solving the problem, it helped me a lot. Out of curiosity, what was wrong with my code, where I added the “Entity” to the "EntityCollection? Maybe in the future I will need a collection :confused: It looks logical and easy in the documentation, but it does not work.

Thank you once again

The EntityCollection you created isn’t attached to the viewer/scene. That’s why I suggested the custom DataSource approach, because there is a mechanism to add that to the viewer while there isn’t one for adding an EntityCollection.