Hi guys,
I am new to WAB(DE), I want to make a widget that removes some layers from the map,
I tried the code below but id does not work,
var layerInfos = LayerInfos.getInstanceSync();
array.forEach(layerInfos.getLayerInfoArray(), function(layerInfo) {
this.map.removeLayer(layerInfo.newSubLayers[0].layerObject);
this.map.removeLayer(layerInfo.newSubLayers[1].layerObject);
this.map.removeLayer(layerInfo.newSubLayers[2].layerObject);
}, this);
What is wrong with it ?
Solved! Go to Solution.
Sara,
As I mentioned in my last reply:
You can not remove a sub layer you have to remove the whole layer.
You can set a sublayer visibility to false but you can not remove a sublayer.
Sara,
You have a big issue with your logic. In your code:
this.map.removeLayer(layerInfo.newSubLayers[0].layerObject);
you are assuming that the layer has sublayers 0 thru 2 and then try and remove the sublayers layer object form the map. You can not remove a sub layer you have to remove the whole layer.
var lyrInfoArr = layerInfos.getLayerInfoArray();
this.map.removeLayer(lyrInfoArr[0].layerObject);
this.map.removeLayer(lyrInfoArr[1].layerObject);
this.map.removeLayer(lyrInfoArr[2].layerObject);
Robert,
your code didn't work because I don't have three layers, I have published ONE mapService that contains three layers, it's giving me "TypeError: Cannot read property 'layerObject' of undefined"
because that is my "lyrInfoArr" variable:
I have tried also:
var layerInfos = LayerInfos.getInstanceSync();
var lyrInfoArr = layerInfos.getLayerInfoArray();
var layerLayer=lyrInfoArr[0].layerObject.layerInfos;this.map.removeLayer(layerLayer[0]);
this.map.removeLayer(layerLayer[1]);
this.map.removeLayer(layerLayer[2]);
But this code does nothing .
Sara,
As I mentioned in my last reply:
You can not remove a sub layer you have to remove the whole layer.
You can set a sublayer visibility to false but you can not remove a sublayer.