Hiding Multiple Feature Layers

571
1
10-25-2011 01:51 PM
BenReilly
New Contributor
Is there a way to use layer.hide(); for multiple layers in a single function? I need to hide several layers at once, and the order they're in seems to be affecting its function. I have this:

   bikeLayer.hide();
   mopedLayer.hide();
   atmLayer.hide();
   recLayer.hide();
   foodLayer.hide();


as part of a larger function. if bikeLayer is turned on, I can hide it. if only atmLayer is on, it won't shut off, with the error that bikeLayer is not defined. How can I do this?
0 Kudos
1 Reply
StephenLead
Regular Contributor III
Is there a way to use layer.hide(); for multiple layers in a single function?... if only atmLayer is on, it won't shut off, with the error that bikeLayer is not defined


Hi Ben,

You could define an array of layers, and add each layer to the array when it is defined. Then iterate through the array and switch off each layer.

Something like this:

//define the array of layers - add each layer as it's created
var mapLayers = [];
mapLayers.add(bikeLayer);
mapLayers.add(atmLayer); //etc

//To switch off the layers
for (var i = 0; i < mapLayers.length - 1; i++) {
  mapLayers.hide()
}


Hope this helps,
Steve
0 Kudos