Remove GeoJSON datasource as well as entities

Hello there!!

I have loaded a GeoJSON datasource with label entity.
Now i have remove the loaded datasource i.e., remove the datasource and as well as label entity(refer below code, i have added label entities with GeoJSON)

Please guide to remove datasource with label entities also.
Refer below code of loaded datasouce with label entites

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]);
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)

}
});
}
});

Thank you in advance.Please guide

Regards,
Shubham M

Hi!

To remove the dataSource, you can use dataSources.remove:

viewer.dataSources.remove(dataSource);

``

There’s an option to destroy to dataSource as well with that function.

To remove the labels, you would could use entities.remove. This would require that you have references to each of the labels you want to remove:

viewer.entities.remove(label);

``

If you just want to remove all the labels and that’s the only type of entities you have, you can remove all of them with removeAll().

I hope this helps!

1 Like

Hi Jane!!

If I use this code say -
viewer.entities.remove(label);

then it is giving error that label is not defined which i think is right.

Since label is the property of entity which cannot be removed this way.

We need to use some another way.

Please suggest. Given solution is not working

Regards,

Shubham M

Haha yes, that is what I meant when I said that you need references to the labels that you want to remove. In your code, it would look something like:

// 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]);
}

``

Hi Jane!!

I also used the array mechanism but in other way. N Your code is also working great.

Thanks alot.

Thanks for helping in every instance.

Regards,

Shubham M

thx for your answer it was very helpful

1 Like