access entity properties from CZML

Easy question to start your day. I want to access entities that have been pulled in from CZML. I can access individual entities using SpaceEventHandlers without trouble. However, I’d like to make some settings universal (e.g. translucencyByDistance) for all entities. I have been using Cesium.LabelGraphics to define individual entities accessed by the SpaceEventHandler. But a similar approach without ‘picking’ has my head spinning. What have I missed? Cheers, erik

------------------------------Sandcastle------------------------------------

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var scene = viewer.scene;

var czml =

[{“id” : “document”,

“version” : “1.0”

},{“id” : “Boston”,

“label”:{“text”:“Boston”},

“position”:{“cartographicDegrees”:[-71.0589,42.3601,0]},

},{“id” : “New York City”,

“label”:{“text”:“New York”},

“position”:{“cartographicDegrees”:[-74.0059,40.7127,0]},

}];

var importedCzml = new Cesium.CzmlDataSource();

importedCzml.load(czml);

viewer.dataSources.add(importedCzml);

importedCzml.entities.label = new Cesium.LabelGraphics({

show: true,

translucencyByDistance: new Cesium.NearFarScalar(25000,0.0,75000,1.0)

});

There’s two issues with your code.

  1. load returns a Promise, so you need to wait for the promise to resolve before there are actually any entities loaded to manipulate.

  2. You need to iterate over the entities to set each label, there’s no general setting that affects everything.

A good example to look at is the “Custom Styling” GeoJSON example: http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=GeoJSON%20and%20TopoJSON.html&label=Showcases Even though it’s using GeoJSON, working with loaded CZML is exactly the same.

Let me know if you still have issues figuring it out.

Thanks again, “clutch” Amato. The simple Sandbox code is pasted below for others to tickle. If anyone has additional thoughts/suggestions, please let me know.

----------------- Sandbox -------------------

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var scene = viewer.scene;

var czml =

[{“id” : “document”,

“version” : “1.0”

},{“id” : “Boston”,

“label”:{“text”:“Boston”},

“position”:{“cartographicDegrees”:[-71.0589,42.3601,0]},

},{“id” : “New York City”,

“label”:{“text”:“New York”},

“position”:{“cartographicDegrees”:[-74.0059,40.7127,0]},

}];

var promise = Cesium.CzmlDataSource.load(czml);

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++) {

//loop through all entities to get data

var entity = entities[i];

var name = entity.label;

entity.label.translucencyByDistance = new Cesium.NearFarScalar(100000,1.0,500000,0.0);

}

}).otherwise(function(error){

//Display any errrors encountered while loading.

window.alert(error);

});

------------------------------ End Sandbox ----------------------------