This is an area of the Entity API that definitely needs some work. Basically, I want the ability to add “highlight” styles so that you can simply tell us what it should look like when selected/moused over instead of having to manually do stuff yourself. For now, the optimal way to do this is to use CallbackProperty. Here’s a full implementation of what you want to do that should be much more performant.
var viewer = new Cesium.Viewer(‘cesiumContainer’);
var highlightedEntity;
var highlightColor = Cesium.Color.GREEN.withAlpha(0.6);
var normalColor = Cesium.Color.YELLOW.withAlpha(0.6);
//A property that returns a highlight color if the entity is currently moused over, or a default color otherwise.
function createCallback(entity){
var colorProperty = new Cesium.CallbackProperty(function(time, result){
if(highlightedEntity === entity){
return Cesium.Color.clone(highlightColor, result);
}
return Cesium.Color.clone(normalColor, result);
}, false);
return new Cesium.ColorMaterialProperty(colorProperty);
}
var promise = Cesium.GeoJsonDataSource.load(’…/…/SampleData/ne_10m_us_states.topojson’);
promise.then(function(dataSource) {
viewer.dataSources.add(dataSource);
//Get the array of entities
var entities = dataSource.entities.values;
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
entity.polygon.material = createCallback(entity);
}
}).otherwise(function(error){
//Display any errrors encountered while loading.
window.alert(error);
});
var scene = viewer.scene;
var handler = viewer.screenSpaceEventHandler;
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && pickedObject.id instanceof Cesium.Entity) {
highlightedEntity = pickedObject.id;
} else{
highlightedEntity = undefined;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);