Why is Legend Widget still updating?

939
2
Jump to solution
12-05-2012 11:10 AM
DougBrowning
MVP Esteemed Contributor
I thought I was getting this figured out but it is not doing what I thought.

In the LegendWidget there is the code

                map.addEventListener(MapEvent.LAYER_ADD, mapLayerAddHandler, false, 1);
                map.addEventListener(MapEvent.LAYER_REMOVE, mapLayerRemoveHandler, false, 1);


If I comment out this code the legend still updates itself.  How does it do this? 

If I comment out the line myLegend.map = map; after these lines that does stop it from updating the legend.  So maybe there is another event buried in the map?  Not sure why there would be two.

I want to know because I want to add a layer to a second TOC when it is checked in the first one.  I thought I would create this event and then work on how to add it to the second TOC (which may be tough since it seems to always connect to the map).

Thanks

doug
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DougBrowning
MVP Esteemed Contributor
EDIT:  Made it more efficient by only setting listeners for nonbasemap layers.

Well I finally found a way to do it.  I tried this before I thought but now it works.

This shows you how to listen for changes in Layer Visibility and also allows for a "Working Set TOC".  This Working Set has just the layers that have been turned on for the current session.

It basically adds an event for every layer to listen for FLEX.Show (showing the layer).  It also builds a start list that contains all the exclude layers and all the map layers so that the TOC starts blank.  Then when the show event kicks it removes that layer from the list of layers to exclude.  All checkmarks and such sync in the two TOCs. The move up and down also syncs though, which i do not want.  I will work on that. 

Hope it helps someone.  If this is wrong let me know.  This is my first Flex work.

1.  Copy the MapSwitcherWidget, paste and rename as MapSwitcherWidget2.
2.  Add a line so that it compiles the MapSwitcherWidget2
3.  In the new MapSwitcherWidget2 modify the initTOC and add a function.  These are the changes

   /* Added by me */
   private function mapLayerAddHandler2(event:Event):void
   {
    templist.removeItemAt(templist.getItemIndex(event.currentTarget.id));
   }
  
private function initTOC(expandLayerItems:Boolean = false):void
            {
   
   
    toc2.map = map;   /* this will also stop layers from being listed */
    toc2.isMapServiceOnly = false; //gotta get this from the config file
    /*toc2.excludeLayers = getExcludeLayers();*/
    toc2.basemapLayers = getBasemapLayers();
   
    templist = getExcludeLayers();
    var layerlist:ArrayCollection = map.layers as ArrayCollection;
    for (var j:int = 0; j < layerlist.length; j++)
    {

     if (toc2.basemapLayers.getItemIndex(layerlist.id) == -1)
     {
      layerlist.addEventListener(FlexEvent.SHOW, mapLayerAddHandler2, false, 0, true);
     }
    
     templist.addItem(layerlist.id);
    
    }

View solution in original post

0 Kudos
2 Replies
DougBrowning
MVP Esteemed Contributor
I have gone through every line of every file that has to do with the TOC and legend and I still can not find the spot where the legend knows a layer has been turned on. 

Anyone know where this happens?  It is not an event listener since it still works without it somehow.
0 Kudos
DougBrowning
MVP Esteemed Contributor
EDIT:  Made it more efficient by only setting listeners for nonbasemap layers.

Well I finally found a way to do it.  I tried this before I thought but now it works.

This shows you how to listen for changes in Layer Visibility and also allows for a "Working Set TOC".  This Working Set has just the layers that have been turned on for the current session.

It basically adds an event for every layer to listen for FLEX.Show (showing the layer).  It also builds a start list that contains all the exclude layers and all the map layers so that the TOC starts blank.  Then when the show event kicks it removes that layer from the list of layers to exclude.  All checkmarks and such sync in the two TOCs. The move up and down also syncs though, which i do not want.  I will work on that. 

Hope it helps someone.  If this is wrong let me know.  This is my first Flex work.

1.  Copy the MapSwitcherWidget, paste and rename as MapSwitcherWidget2.
2.  Add a line so that it compiles the MapSwitcherWidget2
3.  In the new MapSwitcherWidget2 modify the initTOC and add a function.  These are the changes

   /* Added by me */
   private function mapLayerAddHandler2(event:Event):void
   {
    templist.removeItemAt(templist.getItemIndex(event.currentTarget.id));
   }
  
private function initTOC(expandLayerItems:Boolean = false):void
            {
   
   
    toc2.map = map;   /* this will also stop layers from being listed */
    toc2.isMapServiceOnly = false; //gotta get this from the config file
    /*toc2.excludeLayers = getExcludeLayers();*/
    toc2.basemapLayers = getBasemapLayers();
   
    templist = getExcludeLayers();
    var layerlist:ArrayCollection = map.layers as ArrayCollection;
    for (var j:int = 0; j < layerlist.length; j++)
    {

     if (toc2.basemapLayers.getItemIndex(layerlist.id) == -1)
     {
      layerlist.addEventListener(FlexEvent.SHOW, mapLayerAddHandler2, false, 0, true);
     }
    
     templist.addItem(layerlist.id);
    
    }
0 Kudos