Doug
Doug

Reputation: 53

Calling an internal Cesium function from outside the script

I am coding a cesium app and I would like to call an internal cesium function that clears all primitives:

function clearAll() {
    primitives.removeAll();
}

when i do a button press. I know about cesium's built in toolbar buttons but I would like to use an html button I already have in place to call this function on click. It's located in:

<script>
require(['Cesium'], function(Cesium) {
    function clearAll() {
        //code here}
    });
</script>

Any help would be appreciated!

Upvotes: 2

Views: 1071

Answers (1)

emackey
emackey

Reputation: 12418

Take a look at the top of our Billboards example. When Cesium.Viewer is constructed, it provides access to viewer.scene.primitives which includes the .removeAll() function, which is public.

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

function reset() {
    primitives.removeAll();
}

As @Bergi mentions in the comments, typically you don't wire this up with onclick because you need access to scoped variables like the viewer instance. Instead, give your button an id attribute, and use addEventListener (or jQuery) to listen for the button click within that scope.

document.getElementById('myButtonId').addEventListener('click', function() {
    primitives.removeAll();
}, false);

Upvotes: 2

Related Questions