remove some Layers from the map in WAB (DE)

812
3
Jump to solution
11-09-2018 07:53 AM
SaraEL_MALKI
Occasional Contributor II

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 ?

Robert Scheitlin, GISP

ArcGIS Enterprise

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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.

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

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);‍‍‍
0 Kudos
SaraEL_MALKI
Occasional Contributor II

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 .

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.