Removing specified customDataSource

how to remove a customDataSource? for example i have two dataSources, like this below.

var dataSource1 = new Cesium.CustomDataSource('myData1');
var dataSource2 = new Cesium.CustomDataSource('myData2');

var entity1 = dataSource1.entities.add({
   position : Cesium.Cartesian3.fromDegrees(1, 2, 0),
   billboard : {
       image : 'image1.png'
   }
});

var entity2 = dataSource2.entities.add({
   position : Cesium.Cartesian3.fromDegrees(1, 2, 0),
   billboard : {
       image : 'image2.png'
   }
});

viewer.dataSources.add(dataSource1);
viewer.dataSources.add(dataSource2);

what should i do to remove only the dataSource2 ?

Hi @Ahyar,

Thank you for the thoughtful question! I recommend using the remove method of DataSourceCollection to remove dataSource2. Here is the documentation for remove:

https://cesium.com/learn/cesiumjs/ref-doc/DataSourceCollection.html#remove

This should help you get started. Please let me know if you have any other questions or concerns.

Best,
Sam

i tried with this

viewer.dataSources.remove(dataTitik,true)

but it doesn’t work and show this error
Screenshot_20220104_091842
is there any suggestion on my code? thanks

Hi @Ahyar,

Thank you for sharing this with me. It would be helpful if you could provide a more complete code example. For instance, a sandcastle demo that showcases the error would be very helpful. I suspect that this issue stems from the variable dataTitik.

Best,
Sam

i’ve solved it, i think there’s some typos in my html code. but when i tried it on my sandcastle, your suggestion works really well, thanks.

1 Like

@Ahyar

Glad to hear it!

Best,
Sam