How to select an entity using selectedEntity?

Viewed 41

I am trying to select an entity using selectedEntity. I do it like this

btn.onclick = function() {
                for (var i in viewer.dataSources._dataSources) { 
                    if (viewer.dataSources._dataSources[i].name == btns_class[j].value){
                        viewer.selectedEntity = viewer.dataSources._dataSources[i]
                        viewer.zoomTo(viewer.dataSources._dataSources[i])
                    } 
                }
            }

But I get not what I want, the object remains unselected. How can I make an object selected?

Here is what I have:

here is what i have

This is what I want to get:

this is what i want to get

1 Answers

In Cesium, dataSources are collections of entities. You cannot set selectedEntity to be an entire dataSource, instead you must pick just one entity from within the dataSource. So, this line is the main problem:

viewer.selectedEntity = viewer.dataSources._dataSources[i]

Also, the _ underscore prefix indicates Cesium's "private" variables. Ideally one should avoid using those in production code, as they may change without warning between versions of Cesium.

So for example, this will find the named dataSource and select the first entity in the collection:

var dataSource = viewer.dataSources.getByName(btns_class[j].value)[0];
if (Cesium.defined(dataSource)) {
    viewer.selectedEntity = dataSource.entities.values[0];
}

Also, if you want the camera to focus on the entity, you may wish to set viewer.trackedEntity to the same entity. This is similar to a double-click action in Cesium, where selectedEntity and trackedEntity are both set to the target object.

Related