Programmatically Turning Layers Off/On

2342
2
11-09-2010 04:49 AM
RichHammond
New Contributor
We're currently trying to recreate functionality we have in our ArcIMS based mapping application within the ArcGIS Server Flex Viewer. One of the options we currently allow our visitors is to toggle on and off map themes, or predefined groupings of similar layers (i.e. a Recreation theme that will turn on the parks layer, boat ramp layer and trails layer). In ArcIMS we accomplished this through code on the back-end: theme names were passed as a query string in the URL, layers to be turned on and off for each theme were stored in tables within our RDBMS.

We're just getting our feet wet with the Flex Viewer and really are not sure where to begin to replicate this feature. We sorted through the standard widgets, but didn't come across anything that sounded like it would work. Does anyone have any suggestions or ideas that can get us going with this?

Thanks in advance,
Rich Hammond

GIS Team Leader
Florida Center for Community Design and Research
University of South Florida
Tags (2)
0 Kudos
2 Replies
MattMoyles
New Contributor III
You can get all the layers by doing something like this

var layerArray:Array = new Array();
                var mapLyrs:Array = map.layerIds.sort();
                for(var i:Number = 0; i <= map.layerIds.length-1; i++)
                {
                    var layer:Layer = map.getLayer(mapLyrs);
                }
Then you can set layer.visible to turn layers on/off.  Or if you know the name of your layer you can do something like
map.getLayer("LAYERNAME") where you set LAYERNAME in config.xml

<operationallayers>
            <layer label="LAYERNAME" type="feature" visible="false" alpha="1" 
                url="http://url/to/layer/rest/service"/>
</operationallayers>


Additional Info:
You can also check out the LayerList widget.  Which will give you groups of layers provided they are in the same Feature Dataset on ArcServer end.  So if you had a layer like this in your config.xml

<layer label="NOAA Services" type="dynamic" visible="false" alpha="0.5"
                url="http://publicsafetytemplates.esri.com/ArcGIS/rest/services/EMCOP/PublicSafetyFeedSample/MapServer"/>


It would show a collapsible list of all layers under that rest url.  I hope that makes sense.
0 Kudos
KeithPalmer
New Contributor
Rich,

Based on the functionality you are describing, there really isn't a need to replicate the code-based functionality you had in ArcIMS.  Just define the layer groupings in your mxd, add the appropriate layers to those groups and use the MapSwitcher widget (http://help.arcgis.com/en/webapps/flexviewer/help/widgets/mapswitcher_widget.htm) in your application.  Map Switcher will display the grouped layers under the More button and allow the users to turn on/off any  layer groups or individual layers under the groups that they want.

-Keith
0 Kudos