Show only layers visible on map in the layerlist

2471
7
Jump to solution
05-18-2018 04:44 AM
akshayloya
Occasional Contributor II

I have developed an application where we turn on the layer visibility programmatically. These layers are on a web map. Now in the layer list widget, I don't want to show all the layers, I just want to show which I have turned on programmatically. 

The problem is when I configure layerlist, by default I don't add any layers in there. But now when I make the portal layer visibility true, it does not come up in layerlist widget automatically.  How can I achieve this?

Thanks in advance!

Roberts Custom WAB Widgets‌ any suggestions?

0 Kudos
1 Solution

Accepted Solutions
akshayloya
Occasional Contributor II

Maybe I failed to explain my issue correctly. Anyway solved it by simply changing the config value. I was just stressed out today. Thanks

Called the below function:

showLayers: function () {
debugger
// summary:
// create a LayerListView module used to draw layers list in browser.
this.layerListView = new LayerListView({
operLayerInfos: this.operLayerInfos,
layerListWidget: this,
config: this.config
}).placeAt(this.layerListBody);
},

View solution in original post

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Akshay,

Now in the layer list widget, I don't want to show all the layers, I just want to show which I have turned on programmatically. 

This is an issue because the layerlist should show all layer visible or not.

There must be something wrong with how you are setting the layers to be visible for then not to automatically show in the layer list widget as visible. I would have to see some code.

0 Kudos
akshayloya
Occasional Contributor II

This is what I'm doing. I really don't want all the layers in the layerlist as there are 200 of them. I just want the one which I programmatically open. 

var layers = this.map.getLayersVisibleAtScale(this.map.getScale());
array.forEach(layers, lang.hitch(this, function (layer) {
if (layer.name == evt.currentTarget.title) {
if (evt.currentTarget.checked == true) {
layer.setVisibility(true);
} else {
layer.setVisibility(false);
}
}
}));
SO basically when the  layer.setVisibility(true); ....It should come in the layerlist widget also. 
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Akshay,

   So that is your issue then. You do not set the layers visibility directly. You need to use the 

jimu/LayerStructure and jimu/LayerNode

You would use code like this:

define([
...
'jimu/LayerStructure',
...
],
function(
...
LayerStructure,
...

//get the layer structure
this.layerStructure  = LayerStructure.getInstance();

// this loops through all the layers and turns then off
this.layerStructure.traversal(lang.hitch(this, function(layerNode) {
  if(layerNode.title === evt.currentTarget.title){
    layerNode.toggle();
  }
}));
akshayloya
Occasional Contributor II

Maybe I failed to explain my issue correctly. Anyway solved it by simply changing the config value. I was just stressed out today. Thanks

Called the below function:

showLayers: function () {
debugger
// summary:
// create a LayerListView module used to draw layers list in browser.
this.layerListView = new LayerListView({
operLayerInfos: this.operLayerInfos,
layerListWidget: this,
config: this.config
}).placeAt(this.layerListBody);
},
0 Kudos
arunepuri1
New Contributor III

Hi Akshay,

I am having the same issue as you mentioned. But whatever you mentioned in the above code that is already in LayerList\Widget.js.

Can you please explain your solutions in details and from where you called this function. If possible code snippet will be helpful.

I am using WAB 2.8 default Layer List widget.

 

When I add any dynamic layer to the map through another custom widget as per below code.

the map is showing properly with the visible layers which are provided with a SetVisibleLayers option. But the same is not reflecting in LayerList widget and default it is considering map service Default Visibility property and showing all nodes as checked even though on the map that layers are not visible.

 

Code:

I added a sample water network map service 

Water_Network (MapServer) 


var dynamicLayer = new ArcGISDynamicMapServiceLayer(url, {
'visible': true
});
dynamicLayer.setVisibleLayers([-1,-1,20,21,23]);

map.addLayer(dynamicLayer);

Thanks

Arun E

0 Kudos
elogplaza
New Contributor

Solved one of my problems, thanks!

0 Kudos