Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you control visibility of datasource in Cesiumjs?

Tags:

kml

cesium

I want to display multiple datasources in a cesiumjs viewer but need to allow the user to select which ones they want to see at any given time. For example, if I load a kml and a czml file, how do I hide one and show the other? I can't find the cesiumjs way to do this with its API.

like image 854
Mogrifier Avatar asked Mar 21 '15 15:03

Mogrifier


People also ask

Is there a show flag on the DataSource?

Currently there is no show flag on the dataSource, however it is easy to add and remove the dataSource from the list of available dataSources, and this is used to get the show/hide functionality. Here's a working demo: Load the Cesium Sandcastle Hello World example, and paste the following code into the left side, then hit Run (F8).

How to control the show property of data source?

as of now, show is a property of the data source, you can control it by accessing the property in dot or bracket notation:

How do I show/hide a checkbox in cesium Sandcastle Hello World?

Here's a working demo: Load the Cesium Sandcastle Hello World example, and paste the following code into the left side, then hit Run (F8). It should display a checkbox in the upper-left with show/hide functionality.

Should I delete a hidden DataSource?

Also if a dataSource has been hidden a while, you have to consider at what point should you be saving memory by destroying the dataSource rather than continuing to hold onto it (triggering a new lazy load if it is later shown again).


2 Answers

Update Feb 2016: A show flag has been proposed and may be added to a future version of Cesium.

Original answer:

Currently there is no show flag on the dataSource, however it is easy to add and remove the dataSource from the list of available dataSources, and this is used to get the show/hide functionality.

Here's a working demo: Load the Cesium Sandcastle Hello World example, and paste the following code into the left side, then hit Run (F8). It should display a checkbox in the upper-left with show/hide functionality.

var viewer = new Cesium.Viewer('cesiumContainer');

// Create a typical CzmlDataSource.
var dataSource1 = new Cesium.CzmlDataSource();
dataSource1.load('../../SampleData/simple.czml');

// Add a checkbox at the top.
document.getElementById('toolbar').innerHTML =
    '<label><input type="checkbox" id="showCheckbox" /> Show CZML</label>';

var checkbox = document.getElementById('showCheckbox');
checkbox.addEventListener('change', function() {
    // Checkbox state changed.
    if (checkbox.checked) {
        // Show if not shown.
        if (!viewer.dataSources.contains(dataSource1)) {
            viewer.dataSources.add(dataSource1);
        }
    } else {
        // Hide if currently shown.
        if (viewer.dataSources.contains(dataSource1)) {
            viewer.dataSources.remove(dataSource1);
        }
    }
}, false);

This code could be improved, for example it could be a "lazy load" where the dataSource.load does not get called until the first time it's shown. Also if a dataSource has been hidden a while, you have to consider at what point should you be saving memory by destroying the dataSource rather than continuing to hold onto it (triggering a new lazy load if it is later shown again).

like image 51
emackey Avatar answered Nov 09 '22 06:11

emackey


as of now, show is a property of the data source, you can control it by accessing the property in dot or bracket notation:

https://cesiumjs.org/Cesium/Build/Documentation/CzmlDataSource.html#show

const src = new Cesium.CzmlDataSource();
src.show = false;
like image 36
FussinHussin Avatar answered Nov 09 '22 05:11

FussinHussin