3

I am visualizing a geoJSON datasource in Cesium. I have tied it to a checkbox to modify the "show" property in order to turn it on and off after it is added to the viewer (not adding/removing the data source), however it seems like altering the "show" property alone does not actually hide or show the datasource. This is my function (handleclick is the function tied to the checkbox element):

function handleclick(cb) {
if (cb.checked === true) {
    console.log(cb.id + " is checked")
    for (var i in viewer.dataSources._dataSources) {
        if ("chk_" + viewer.dataSources._dataSources[i]._name == cb.id) {  //match checked box to datasource ID
            viewer.dataSources._dataSources[i].show =  new Cesium.ConstantProperty(true)  //show layer?             
        }
    }       
} else {
    console.log(cb.id + " is unchecked")
    for (var i in viewer.dataSources._dataSources) {
        if ("chk_" + viewer.dataSources._dataSources[i]._name == cb.id) { 
            viewer.dataSources._dataSources[i].show =  new Cesium.ConstantProperty(false)  //hide layer?                
        }
    }
}

Do I need to update the viewer somehow after the "show" property (referenced at https://cesiumjs.org/Cesium/Build/Documentation/GeoJsonDataSource.html#show) is changed in order for the data to be hidden?

1 Answer 1

2

The function works, just need to change the show property to the actual boolean value:

viewer.dataSources._dataSources[i].show =  true
2
  • That's correct. Generally we discourage use of "private" variables, ones with leading underscores like _dataSources[i]. Instead you can use things like viewer.dataSources.length and viewer.dataSources.get(i).
    – emackey
    Commented May 3, 2019 at 15:37
  • Thanks for the feedback. The reason I'm using the underscores is that what the JSON properties show on my console log. Is that a setting I would change?
    – GIS87
    Commented May 3, 2019 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.