How to add global event for specific entities?

Hi, I am really new to Cesium and have this problem while transferring from Google Earth to Cesium.

I have several entities from loaded KML files. When these files are loaded and added to the viewer, I want to have an event incurred when any one of these entities is clicked/selected.
How could I do that? Before that, we have
google.earth.addEventListener(ge.getGlobe(), 'click', clickAction)
to set up the onClick action. Do we have anything similar to this function?

Thanks.

I think you can use the general hit-test for KML entities too, see e.g. http://analyticalgraphicsinc.github.io/cesium-google-earth-examples/examples/hittest.html

Willem

Unfortunately this feature slipped from the public to the private API a couple versions ago (issue 2734), but you can still access it. NOTE that accessing private API is subject to change without warning in future versions.

Start by loading Cesium Sandcastle, and then paste in this code:

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

viewer.dataSources.add(Cesium.CzmlDataSource.load('../../SampleData/simple.czml'));

Cesium.knockout.getObservable(viewer, '_selectedEntity').subscribe(function(entity) {
    if (!Cesium.defined(entity)) {
        console.log('De-selected entity.');
    } else {
        console.log('Selected entity ' + (entity.name || entity.id));
    }
});

Switch to the “console” tab (at the bottom) so you can see console messages being written when entities are selected and de-selected.

     --Ed.

I did a little test on the code and find it worked on the Click Event. Then, how should I get the KML entities information, like name or id or description?

Hi Ed, for Cesium 1.16, is Cesium.knockout.getObservable(viewer, '_selectedEntity') still the suggested approach to listen for changes of the viewer.selectedEntity property? I'm asking because it seems I cannot listen to the selectedEntity event directly.

Thanks!

Yes, that’s still current. Any change in status would be marked in issue 2734. It’s low priority because the workaround is just to ignore the fact that it’s private now, and listen for the changes just like before when it used to be public. The addition of that underscore is the only thing that’s different.

–Ed.

Thanks Ed, good to know!

--Norman