Removing Geoserver KML points from view

Hello all,
figure it out how to add points from Geoserver, not as xxx.kml file but as wms/kml?layers.
But now have a “little” problem with removing selected poi layer from view.
For each poi layer using same button for add/remove points, first click add points, and second click remove points from scene.
Everything works fine with adding layers, but cannot make it work to remove just that clicked layer from scene…so far only removeAll works … but that’s not what I need since it removes all shown poi layers.
Read other posts about removing KML but cannot reproduce it in my case, as you can see in code.
+++++ Working code with removeAll +++++
var viewer = new Cesium.Viewer(‘cesiumContainer’, {
// sceneMode : Cesium.SceneMode.COLUMBUS_VIEW,
sceneMode : Cesium.SceneMode.SCENE3D,
animation : false,
timeline : false,
infoBox : true,
baseLayerPicker: false,
sceneModePicker: false,
imageryProvider: false,
vrButton : true,
homeButton : false
});
//------------------------- KML POI initialization ------------------------------------------------
var optionsKML = {
camera : viewer.scene.camera,
canvas : viewer.scene.canvas,
clampToGround: true
};
//var dataSource1 = new Cesium.KmlDataSource();
//dataSource1.load(‘http://xxxx.xxxx.xxx/geoserver/wms/kml?layers=castles_3d1’, optionsKML);
//------------------------- KML POI initialization ------------------------------------------------
… code code code …
//----------------------on/off poi kml castles … -------------------------------------
var dvorci;
function poi1() {
if (dvorci) {
// viewer.dataSources.remove(dataSource1);
viewer.dataSources.removeAll();
dvorci = null;
} else {
// dvorci = viewer.dataSources.add(dataSource1);
dvorci = viewer.dataSources.add(Cesium.KmlDataSource.load(‘http://xxxx.xxxx.xxx/geoserver/wms/kml?layers=castles_3d1’, optionsKML));
}
}
**//----------------------on/off poi kml castles … -------------------------------------
//----------------------on/off Alpine huts … ------------------------------------- **
var pk;
function hut() {
if (pk) {
viewer.dataSources.removeAll();
pk = null;
} else {
pk = viewer.dataSources.add(Cesium.KmlDataSource.load(‘http://xxxx.xxxx.xxx/geoserver/wms/kml?layers=Alpine_huts’, optionsKML));
}
}
**//----------------------on/off Alpine huts … -------------------------------------**Hope the explanation of the issue is clear enough !Please help me to fix this issue.

Thx in advance,
Davor

Davor, I’m sorry no one ever replied to this. The problem is most likely related to how you’re loading the data source. viewer.dataSources.add does not return the datasource itself, but rather a promise to the datasource, so when you remove the datasource, you need to pass the datasource and not the promise. You can modify your example to set the actual data source and everything should work:

viewer.dataSources.add(Cesium.KmlDataSource.load(‘http://xxxx.xxxx.xxx/geoserver/wms/kml?layers=Alpine_huts’, optionsKML))

.then(function(dataSource) {

pk = dataSource

});

Let me know if you’re still having issues and I can link to a more complete example.

Thanks

It works !!!

Thank you very much Matthew :slight_smile:

Davor

Auto Generated Inline Image 1.jpg