How to check visibility of entity?

I have added a polyline using:

viewer.entities.add(…)

How to I show and hide with a button by checking to see if entities are visible or not?

The following check does not work.

if(viewer.entities.values[0].polyline.show) {

viewer.entities.values[0].polyline.show = new Cesium.ConstantProperty(false);

} else {

viewer.entities.values[0].polyline.show = new Cesium.ConstantProperty(true);

}

The above does not work. I also tried…

if(viewer.entities.values[0].show) {

viewer.entities.values[0].show = new Cesium.ConstantProperty(false);

} else {

viewer.entities.values[0].show = new Cesium.ConstantProperty(true);

}

What is the right way?

I have done just
entity.show=false;

try

if(viewer.entities.values[0].show) {

viewer.entities.values[0].show = false;

} else {

viewer.entities.values[0].show = true;

}

Sai, the reason your initial code does not work is because polyline.show is not a boolean but an object. You would need to do polyline.show.getValue(time) to retrieve the boolean value.

That being said, if you want to toggle an entire entity on/off, Brian has the right idea and entity.show is actually a simply boolean value (because it’s meant for just this type of UI use case).

If all you care about is toggling, a would change Brian’s code to

var entity = viewer.entities.values[0];

entity.show = !entity.show;

which avoid repeated lookups and avoid the need for if logic.