How to automatically remove entity polygon by id?

when i create a new entity polygon i want the old entity polygon to be removed i am using

viewer.entities.removeAll();
                    // Create a label entity for each feature
                    viewer.entities.add({
                        polygon: {
                            hierarchy: Cesium.Cartesian3.fromDegreesArray(features[0].data.geometry.rings[0].flat()),
                            material: Cesium.Color.DEEPSKYBLUE.withAlpha(0.8),
                        },
                    });

put in front of the function to delete the old polyogon when running the function again but it is inconvenient because it will delete the label belonging to the entity collection, is there any other way to remove the polygon before creating a new one?

var myEntity = viewer.entities.add({ ... });
...
viewer.entities.remove(myEntity);

Cheers,

Alex

for my example : Cesium Sandcastle
Left click to highlight and show info feature

i am using removeall command to remove old polygon when a new polygon is created, but also it will remove non-polygon entities, i tried your way but it doesn’t work, or am i misplaced ?

:sob::sob::sob::sob::sob::sob:

Hiya,

“My way” definitly works, but I don’t know what you did. But maybe this is a problem with your overall design more than anything. There’s no need to use removeAll() at all if it’s a simple selector, but (looking at your example) you’ve got all your code running inside an event handler, however a selector needs to live outside of it, something like;

var mySelector = null;
...
viewer.screenSpaceEventHandler.setInputAction( ... ) {
   if ( mySelector ) {
      viewer.entities.remove( mySelector );
      mySelector = null; // just to make sure
   }
   mySelector = viewer.entities.add( ... );
}

Cheers,

Alex

thank you, let me try again

It’s work, thank you very muchhh

one more question,how to use only LEFT-CLICK to display infobox for Label and ImageryLayerFeatures, as in my example I am using LEFT-CLICK + CTRL key to select Label

You’ve set it up yourself with an additional parameter ‘1’, which is CTRL key. For just left-click, ommit it. More here; ScreenSpaceEventHandler - Cesium Documentation

Cheers,

Alex

sorry for my stupid questions :((, but i want to use Left-click for 2 cases, show info youtube play or infobox , now i use Left click, and Left-click + ctrl, should i use if in function

viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
},LEFT-CLICK);

to split the case click on ImageryLayerFeatures or billboard entity?

i found the way, i used if (!Cesium.defined(pickedFeature))