How do I remove desired group of entities?

Hello all,
I have been trying to find the solution for this problem for a long time, and I have searched for hours on the forum but could not find a neat and effective solution.

– I have a group of points and large number of polylines (thousands) and I want to remove them but not any other entities created such as spheres, ellipsoids or whatever.

– I tried to create parent entity, but when I remove the parent, children are not removed

eg.
viewer.entities.add({

parent : pts,

position : cartesian, //defined variable

point : {

pixelSize : 15,

color : Cesium.Color.RED

}

});

viewer.entities.add({

position : Cesium.Cartesian3.fromDegrees(-102.0, 45.0, 500000.0),

ellipsoid : {

radii : new Cesium.Cartesian3(45000.0, 45000.0, 90000.0),

material : Cesium.Color.fromRandom({alpha : 1.0})

}

});

var pts = viewer.entities.add(new Cesium.Entity());
viewer.entities.remove(pts);

``

This does not remove anything from the scene.

whereas,

var pts = viewer.entities.add(new Cesium.Entity());
viewer.entities.add({

parent : pts,

position : cartesian,

point : {

pixelSize : 15,

color : Cesium.Color.RED

}

});

viewer.entities.add({

position : Cesium.Cartesian3.fromDegrees(-102.0, 45.0, 500000.0),

ellipsoid : {

radii : new Cesium.Cartesian3(45000.0, 45000.0, 90000.0),

material : Cesium.Color.fromRandom({alpha : 1.0})

}

});

viewer.entites.removeAll();

This removes all the entities including the ellipsoid.

– Also, the documentation is also not very helpful.

So, if anyone could put this question to rest once and for all, it will be great.

Thanks,

Parthesh.

I found a work-around by using “viewer.entities.removeById()”, but I have to run a for loop to give id’s to the entities and again a for loop to remove them. But still it would be great to have a single function to do this.

Hello Parthesh,

When you call viewer.entities.add, it returns the entity that was added. You can use that entity to the call to viewer.entities.remove. Just store your point entities in an array when you add them.
Here is an example:

var points = ;
points.push(viewer.entities.add({
parent : pts,
position : cartesian,
point : {
pixelSize : 15,
color : Cesium.Color.RED
}
}));

for (var i = 0; i < points.length; i++) {
viewer.entities.remove(points[i]);
}

``

Hope this helps!

Hannah

You could also add them to a CustomDataSource then remove then add/remove the CustomDataSource from viewer. I find that easier to manage if you have multiple groups of entities.