Turn off sublayer A, sublayer B will turn off too

236
9
09-09-2013 07:15 AM
MayJeff
Occasional Contributor
Is there any simple way to uncheck sublayer A on TOC  then sublayer B will automatically turn off based on sublayer A?  On the other hand, if sublayer A is turn on then sublayer b will turn on too.

Thank you.
Tags (2)
0 Kudos
9 Replies
DougBrowning
MVP Frequent Contributor
0 Kudos
MayJeff
Occasional Contributor
I saw the thread you answered here:
http://forums.arcgis.com/threads/89934-LayerList-Widget-Turn-on-Sublayers?p=319435&viewfull=1#post31...
Can you show me how do you setup a listener on the Show of a layer you mentioned on the thread?  I can't figure out of this.
Thank you.
0 Kudos
DougBrowning
MVP Frequent Contributor
The code was in the last post of the thread.  Not sure what else you need.  Look at the top of the thread for the code to make a layer visible -

In your case I would probably setup a Show listener on layer B and then on that event make layer A visible.  And then setup a Show listener on layer B to turn on A.  Then add 2 listeners on the Hide event (I think it is called hide) to do that same things but turn off the visible.

The code is layerlist.addEventListener(FlexEvent.SHOW, mapLayerAddHandler2, false, 0, true);
where mapLayerAddHandler2 is your function.  j would be the index of the A layer and B layer.  If you do not know the index you can look it up.  I have seen the code to lookup the layer index somewhere on the forum but I can not find it now.

If your layers do not become visible try to add this to refresh the TOC  toc.validateNow()

Hope that helps.
0 Kudos
MayJeff
Occasional Contributor
Thanks for replying my thread.  Do you add both inside TocItemRebderer.as? or add the event listener inside TocMapLayerItem.as?  My problem is not sure where to add the function too. 
Thank you.
0 Kudos
DougBrowning
MVP Frequent Contributor
I set mine in the Init of a custom widget.  For you I think you could put it in either files Init section and it should get run when Flex is loading the map.  Anywhere it will get run on startup should work.  Sorry I am not that experienced at flex yet.
0 Kudos
MayJeff
Occasional Contributor
Thank you information.  Actually I would like to be able to control layer visibility on TOC checkbox by clicking it to turn on/off sublayer.  If sublayer A is on then sublayer B will on.
0 Kudos
DougBrowning
MVP Frequent Contributor
That is what the show event does.  You are just setting up the event listener in the init.  Then the procedure you create is called when the event happens (in this case when the box is checked).
0 Kudos
MayJeff
Occasional Contributor
Thank you for replying my thread again,  Really appreciate your time.  I think I got some ideas but still unclear on some part.
Even though there are in separate group layer? I added the code your mention to my init function on TOCWidget.mxml
Can you change to specific layer id for sublayer A (i.e. layerlist[10])?
Code 1:
//new add -----------------
    var layerlist:ArrayCollection = map.layers as ArrayCollection;
    for (var j:int = 0; j < layerlist.length; j++)
    {
     if (toc.basemapLayers.getItemIndex(layerlist.id) == -1)
     {
      layerlist.addEventListener(FlexEvent.SHOW, mapLayerAddHandler2, false, 0, true);
     }
    }
    //end new add ----------------------

code 2:


   private function mapLayerAddHandler2():void
   {
    //Try to turn off sublayer B if sublayer A is being turn off
   }
0 Kudos
DougBrowning
MVP Frequent Contributor
In the code 1 section that codes sets up a listener for ALL layers - you just want A and B so adjust accordingly.

On Code 2 section I do not know how.  There is code in TOCMapLayerItem.as of setVisible(layer.visible, true);  so that may work if you are in that file.  I think there is a .visible property in the layer somewhere.  Search the forums I am sure it is on there.  Or just look through all the properties of the layer object and find it and try it.  Flex can be a lot of trial and error when you start.

Like I said I am new so that is all the help I got.
0 Kudos