Determine which operational layers are visible

3020
3
11-11-2014 10:01 AM
RobertParis
New Contributor

I am running a FlexViewer 3.6 which contains a number of operational feature layers. The user is able to turn operational layers on or off using the built in More... button and clicking on or off the appropriate checkbox next to each layer name. I have a custom widget that needs to be able to determine whether an operational layer is Checked On or Off. I have searched all over the place and have realized it the answer is within the viewer and not the map service since I need to current state based on user interaction and not the configured state.

I believe the answer lies in TOC Items but would appreciate any assistance in simplifying this for more. Ultimately an array with the layer id or label and a bit for visible or not is what I am looking for.

Thanks in advance for any help!

Robert.

0 Kudos
3 Replies
AnthonyGiles
Frequent Contributor

Robert,

I don't think there is any function built into the code that allows you to determine this. I ran into the same problem over a year ago when I was trying to write a widget that saved the current state of the viewer. In the end I had to write some code that looped through all the dynamic map services then checked which layers in each were visible. The code sample is as follows:

var acVisLayers:ArrayCollection = new ArrayCollection();

for each (var layer:Layer in map.layers)

                {

                    var layVisAc:ArrayCollection;

                    if (layer is ArcGISDynamicMapServiceLayer)

                    {

                        layVisAc = ArcGISDynamicMapServiceLayer(layer).visibleLayers as ArrayCollection;

                    }

                    var lvisObj:Object = {name: layer.name,visible: layer.visible,visarray: layVisAc};

                    acVisLayers.addItem(lvisObj);

                }

Regards

Anthony

0 Kudos
RobertParis
New Contributor

Thanks for your reply Anthony.

I will review your code and try things at my end. I am a bit concerned though since my config doesn't define any Dynamic Map Service Layers with sub-layers, rather just Feature layers one after the other with no grouping. For example:

<operationallayers coordSystemID="102187" coordSystemName="NAD_1983_3TM_114">

  <layer label="Location - ATS" type="feature" visible="true"  alpha="1"

  url="http://server.com/arcgis/rest/services/XYZ/XYZ_ABCD/MapServer/0"

  assess="True" constraintType="Geog" constraintSubType="Provincial" fieldAttribute1="TWP" fieldAttribute2="SC"/>

  <layer label="Airport Base" type="feature" visible="true"  alpha="1"

  url="http://server.com/arcgis/rest/services/XYZ/XYZ_ABCD/MapServer/2"

  assess="False" constraintType="" constraintSubType="" fieldAttribute1="" fieldAttribute2=""/>

  <layer label="Assessment Area" type="feature" visible="true" alpha="1"

  url="http://server.com/arcgis/rest/services/XYZ/XYZ_ABCD/MapServer/8"

  assess="True" constraintType="System" constraintSubType="Test" fieldAttribute1="AssessArea" fieldAttribute2="AssessArea"/>

</operationallayers>

Finally, in case I wasn't clear in original post, I need the current state and not the state as specified in the config file.

Thanks again for your help.

Robert.

0 Kudos
AnthonyGiles
Frequent Contributor

Robert,

have a look at this post to see if any of the code can help:

Save layer visibility preferences - flex code question

Regards

Anthony

0 Kudos