Layer1 and Layer 2 are not instances of KmlDataSource, they are Promises to the data source instance once it’s down loading. If you are not familiat with Promises, I recommend reading this article: http://www.html5rocks.com/en/tutorials/es6/promises/ Cesium specifically uses the when
promise library that is mentioned in the article.
I rewrote your sample code in two ways, first to do what you want using promises, and second to avoid promises and simply create instances of KmlDataSource. In such a simple example, promises don’t get you much (other than letting you know when the data is ready to go). But when you need to wait for the data to be loaded before taking action, promises are really useful.
//This version uses promises
var viewer = new Cesium.Viewer(‘cesiumContainer’);
var promise1 = Cesium.KmlDataSource.load(’…/…/SampleData/kml/facilities/facilities.kml’);
var promise2 = Cesium.KmlDataSource.load(’…/…/SampleData/kml/gdpPerCapita2008.kmz’);
Cesium.when(promise1, function(dataSource1){
Sandcastle.addToolbarButton(‘Add DataSource 1’, function() {
viewer.dataSources.add(dataSource1);
});
Sandcastle.addToolbarButton(‘Remove DataSource 1’, function() {
viewer.dataSources.remove(dataSource1);
});
});
Cesium.when(promise2, function(dataSource2){
Sandcastle.addToolbarButton(‘Add DataSource 2’, function() {
viewer.dataSources.add(dataSource2);
});
Sandcastle.addToolbarButton(‘Remove DataSource 2’, function() {
viewer.dataSources.remove(dataSource2);
});
});
//And this version uses new KmlDataSource(). Load still returns a promise in this case, but we just ignore it.
var viewer = new Cesium.Viewer(‘cesiumContainer’);
var dataSource1 = new Cesium.KmlDataSource();
dataSource1.load(’…/…/SampleData/kml/facilities/facilities.kml’);
var dataSource2 = new Cesium.KmlDataSource();
dataSource2.load(’…/…/SampleData/kml/gdpPerCapita2008.kmz’);
Sandcastle.addToolbarButton(‘Add DataSource 1’, function() {
viewer.dataSources.add(dataSource1);
});
Sandcastle.addToolbarButton(‘Remove DataSource 1’, function() {
viewer.dataSources.remove(dataSource1);
});
Sandcastle.addToolbarButton(‘Add DataSource 2’, function() {
viewer.dataSources.add(dataSource2);
});
Sandcastle.addToolbarButton(‘Remove DataSource 2’, function() {
viewer.dataSources.remove(dataSource2);
});