Select to view content in your preferred language

Hiding multiple feature layers

1177
4
Jump to solution
12-21-2011 03:07 PM
EdSaunders
Regular Contributor
Hi all,

I've add some feature layers and pushed them into an array like below:

var dwf = "http://<server>/ArcGIS/rest/services/SLR/Water/MapServer/1";
dwfLayer = new esri.layers.FeatureLayer(dwf, {
id : "dwf",
title : "Drinking water facilities",
infoTemplate : infoTemplate
});
legendLayers.push({
layer : dwfLayer,
title : 'Drinking water facilities'
},

Then, in another function, I'd like to switch off all the feature layers so I've tried using:

var len = legendLayers.length;
for (var i = 0; i < len; i++) {
legendLayers.hide();
}

I've also tried:

dojo.forEach(legendLayers.layer, function(layer) {
layer.hide();
});

But neither of the above functions work to hide all the layers in the legendLayers array.  I'm obviously missing something. Can anyone help?

Thanks heaps,
Ed
0 Kudos
1 Solution

Accepted Solutions
StephenLead
Honored Contributor

but I get the error message "activeLayers.hide is not a function" using:

activeLayers.push([saLayer, wpLayer, dwpLayer, roadsLayer, wfLayer, dwfLayer]);
activeLayers.push({layer:saLayer},{layer:wpLayer},{layer:dwpLayer},{layer:roadsLayer},{layer:wfLayer},{layer:dwfLayer});



Try using push within a for loop, in the same way as you're using hide?

View solution in original post

0 Kudos
4 Replies
StephenLead
Honored Contributor

var len = legendLayers.length;
for (var i = 0; i < len; i++) {
 legendLayers.hide();
}


Hi Ed,

It looks like you may need another array to hold the feature layers, rather than trying to re-use the one holding the legend layers. You could try something like this:

var fLayers = []; //define a new array for the feature layers
legendLayers.push({
  layer : dwfLayer,
  title : 'Drinking water facilities'
}
fLayers.push(dwfLayer); //push this layer to the new array (as well as the Legend array)

//Iterate through the feature layers and switch them off
for (var i = 0; i < fLayers.length; i++) {
  var layer = fLayers;
  layer.hide();
}


Cheers,
Steve
0 Kudos
EdSaunders
Regular Contributor
Steve, thanks for the input mate.  I didn't think it mattered when and how you add layers into an array but it seems it's quite important!  For example, this works:

activeLayers.push(saLayer);
activeLayers.push(wpLayer);
activeLayers.push(dwpLayer);
activeLayers.push(roadsLayer);
activeLayers.push(wfLayer);
activeLayers.push(dwfLayer);

using the loop:

var len = activeLayers.length;
for (var i = 0; i < len; i++) {
  activeLayers.hide();
}

but I get the error message "activeLayers.hide is not a function" using:

activeLayers.push([saLayer, wpLayer, dwpLayer, roadsLayer, wfLayer, dwfLayer]);
activeLayers.push({layer:saLayer},{layer:wpLayer},{layer:dwpLayer},{layer:roadsLayer},{layer:wfLayer},{layer:dwfLayer});

Can I not put layers into an array using the two types of syntax above?

Cheers,
Ed
0 Kudos
StephenLead
Honored Contributor

but I get the error message "activeLayers.hide is not a function" using:

activeLayers.push([saLayer, wpLayer, dwpLayer, roadsLayer, wfLayer, dwfLayer]);
activeLayers.push({layer:saLayer},{layer:wpLayer},{layer:dwpLayer},{layer:roadsLayer},{layer:wfLayer},{layer:dwfLayer});



Try using push within a for loop, in the same way as you're using hide?
0 Kudos
EdSaunders
Regular Contributor
Yeah I was thinking the same thing. I was trying it the same way as map.addLayers. I'll use a loop there as well because I've got a few more layers to add. Thanks mate, have a good Xmas break!
0 Kudos