Remove GeoJSON datasource as well as entities

// 1. we can save the references to the entities that are labels in an array called 'labels'
var labels = [];


var promise = Cesium.GeoJsonDataSource.load('../../../cesiumLayers/sampledata/nextgen/World_Lables.geojson');
promise.then(function (dataSource) { 
    //viewer.dataSources.add(dataSource); 
    var entities = dataSource.entities.values; 

    for (var i = 0; i < entities.length; i++) { 
        var entity = entities[i]; 
        var abc = entity.position.getValue(); 
        var stPt = convertCartesianToCartographic(abc); 
        //entity.position = Cesium.Cartesian3.fromDegrees(stPt[0],stPt[1],stPt[2]); 

        // 2. Now, push each entity that's a label to our 'labels' array
        labels.push(viewer.entities.add({ 
            position: Cesium.Cartesian3.fromDegrees(stPt[0], stPt[1], stPt[2]), 
            label: { 
                text: entity.properties.name, 
                font: '16px Helvetica', 
                fillColor: Cesium.Color.WHITE, 
                outlineColor: Cesium.Color.BLACK, 
                outlineWidth: 5, 
                //pixelOffset : new Cartesian3(50.0, -50.0), 
                style: Cesium.LabelStyle.FILL_AND_OUTLINE, 
                translucencyByDistance: new Cesium.NearFarScalar(2.5e6, 1.0, 2.5e7, 0.0) 
            } 
        })); 
    } 
});

// some code later...

for (var i = 0; i < labels.length; i++) {
    // remove each entity that's a label
    viewer.entities.remove(labels[i]);
}
Share

Leave a Reply

Your email address will not be published. Required fields are marked *