chatzich
chatzich

Reputation: 1133

How to control the visibility to Cesium/CZML DataSources

I have a CZML datasource declared:

public geometryDataPromise: Cesium.CzmlDataSource;

I am loading it with an endpoint above when a component is loaded:

    if(!this.store.geometryDataPromise) {
        this.store.geometryDataPromise = Cesium.CzmlDataSource.load(environment.apiBaseURL + `/protectedareas/geometry/all`);
    }

all the rendered objects are displayed to terrain but trying to follow the instructions by doing:

this.store.geometryDataPromise.show = false;

the objects are not being hidden

Upvotes: 1

Views: 832

Answers (2)

chatzich
chatzich

Reputation: 1133

First I have to take the result of the Cesium.CzmlDataSource.load promise

Cesium.when(Cesium.CzmlDataSource.load(environment.apiBaseURL + `/protectedareas/geometry/all`), result => {
        this.sources = result;
        this.viewer.dataSources.add(this.sources);

});

and then just change it's fiend show when the visibility changed

this.store.sourceVisibility.subscribe(visibility=>this.sources.show=visibility);

Upvotes: 0

emackey
emackey

Reputation: 12448

The problem here is that Cesium.CzmlDataSource.load does not return a Cesium.CzmlDataSource. It returns a Promise to asynchronously go get a CzmlDataSource, and that's not the same thing at all. Your code is trying to show or hide the promise, that's not a thing that gets displayed.

var dataSourcePromise = Cesium.CzmlDataSource.load( ... );
var dataSource = null;

dataSourcePromise.then(function(d) { dataSource = d; });

Note that after the above code runs, dataSource will be null for some time while the browser waits for the server's response to finish downloading. Once the callback function fires, the dataSource is ready.

function onClick() {
    if (dataSource !== null) {
        dataSource.show = !dataSource.show;
    }
}

You can wire up a click handler for a toggle button like this. But the toggle won't do anything until after the dataSource is downloaded and ready.

Upvotes: 2

Related Questions