Dynamic Legend widget for Flexviewer 2.0

5054
65
07-29-2010 01:18 PM
RobertScheitlin__GISP
MVP Emeritus
All,

    I am happy to announce the first community developed (and shared on the code gallery) widget for the Flexviewer 2.0. I have finalized my Dynamic Legend Widget for the 2.0 FlexViewer and shared it for all to use.

This version is COMPLETELY dependent on 2 things

  1. ArcGIS Server 10 map services

  2. Flex Viewer 2.0 and obviously Flex API 2.0


I have made it XML configurable this time so that you can specify that certain map services or certain layers in a map service are not to be displayed in the widget.

It is called dynamic because the legend will only draw map services and layer that are visible in your map. It respects scale dependency of layers and if the layer is checked in the TOC or operational layer TOC. The legend automatically redraws when layers have been checked/unchecked and when zooming in or out and a layer scale dependency has been meet.

I hope you enjoy it and I HOPE that you read and understand that it will not work with mapservices that are not served from a ArcGIS 10 Server.

http://www.arcgis.com/home/item.html?id=f60406b9f52340d59eb557bf64136af6

PS. don't forget to rate the code.
Tags (2)
0 Kudos
65 Replies
RobertScheitlin__GISP
MVP Emeritus
Kirti.

  Since you already have the 1.x version working it will not be hard for me to provide you with the code to replace. I will post something hopefully later today.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Kirti,

   Try this

//Add this import at the top with the others
import mx.binding.utils.ChangeWatcher;

//replace your existing function with this one
private function showStateLegend(event:MouseEvent):void
   {
    WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
    //This is where we determine if this is the first call to make
    //the legend or do we just need to update it
    if (legendRepeater.dataProvider != null){
     updateLegend();
    } else {
     showMessage("Please be patient while the Legend is generated...", true);
     var liveLayers:Array = getLayers(); 
     var url:String; 
     for (var i:int = 0; i < liveLayers.length;i++) {
      if (liveLayers is ArcGISDynamicMapServiceLayer) {
       if (ArcGISDynamicMapServiceLayer(liveLayers).id != "Aerial Photography")
       {
        requestCount++;           
        url = ArcGISDynamicMapServiceLayer(liveLayers).url.replace("rest/", "");
        webService.generateLegends.send(url, 96, 20, 20, ArcGISDynamicMapServiceLayer(liveLayers).id);
        ChangeWatcher.watch(ArcGISDynamicMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
       }         
      } else if (liveLayers is ArcGISTiledMapServiceLayer) {
       requestCount++;
       url = ArcGISTiledMapServiceLayer(liveLayers).url.replace("rest/", "");
       webService.generateLegends.send(url, 96, 20, 20, ArcGISTiledMapServiceLayer(liveLayers).id);
       ChangeWatcher.watch(ArcGISTiledMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
      }     
     }     
    }
   }
0 Kudos
TomJones
Emerging Contributor
My apologies Robert, I really am a newbie to flex and currently quite confused :(.

I used the dynamic legend sample at http://resources.esri.com/arcgisserver/apis/flex/index.cfm?fa=codeGalleryDetails&scriptID=16838.
I then used the code from LegendExample.mxml and added it to a live layer that we have created. This is shown below,

private function showStateLegend(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 4, 400);
             LoadLegend();
}

private function LoadLegend() : void
{
displayMessage("Please wait while the legend loads", true);
lr.addEventListener(LegendEvent.COMPLETE, handleLegendComplete);
   
// to get the soap url from the rest url, simply replace the word "rest"
soap_url = rest_url.replace("/rest/services/","/services/");
r.RetrieveLegend(soap_url);
}

public function handleLegendComplete(event:LegendEvent) : void
{   
var leg:Legend = new Legend();
leg.initialize();
leg.LegendData = lr.getLegendInfo();
   
PopUpManager.addPopUp(IFlexDisplayObject(leg), this, false);    leg.move(8, 115);
leg.height = 500;
}  

I made no other changes to the remainder of the files included in the sample.

So, I'm unsure where exactly to place this bit of code you suggested, as there is no "updateLegend()" function anywhere within the code.

Sorry for the trouble.



Kirti,

   Try this

//Add this import at the top with the others
import mx.binding.utils.ChangeWatcher;

//replace your existing function with this one
private function showStateLegend(event:MouseEvent):void
   {
    WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
    //This is where we determine if this is the first call to make
    //the legend or do we just need to update it
    if (legendRepeater.dataProvider != null){
     updateLegend();
    } else {
     showMessage("Please be patient while the Legend is generated...", true);
     var liveLayers:Array = getLayers(); 
     var url:String; 
     for (var i:int = 0; i < liveLayers.length;i++) {
      if (liveLayers is ArcGISDynamicMapServiceLayer) {
       if (ArcGISDynamicMapServiceLayer(liveLayers).id != "Aerial Photography")
       {
        requestCount++;           
        url = ArcGISDynamicMapServiceLayer(liveLayers).url.replace("rest/", "");
        webService.generateLegends.send(url, 96, 20, 20, ArcGISDynamicMapServiceLayer(liveLayers).id);
        ChangeWatcher.watch(ArcGISDynamicMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
       }         
      } else if (liveLayers is ArcGISTiledMapServiceLayer) {
       requestCount++;
       url = ArcGISTiledMapServiceLayer(liveLayers).url.replace("rest/", "");
       webService.generateLegends.send(url, 96, 20, 20, ArcGISTiledMapServiceLayer(liveLayers).id);
       ChangeWatcher.watch(ArcGISTiledMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
      }     
     }     
    }
   }
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tom,

   OK, That code is not mine, so I can not help with that. You will have to wait for this functionality until you switch to flexviewer 2.0.
0 Kudos
TomJones
Emerging Contributor
Is migrating to flex 2.0 the only solution.  😞

Thanks for all your help, Robert.
It's highly appreciated. 🙂
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tom,

    I am afraid so unless the developer of the code you are using adds the same ability you are looking for.
0 Kudos
TomJones
Emerging Contributor
Robert,

Did you perhaps write your own code for creating a dynamic legend?

Will you be willing to attach or post the complete code on this thread, please. I am really struggling to try and modify another developer's code.

Tom
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tom,

   Here is the link

Be advised that this is not a simple thing to add...

http://forums.esri.com/Thread.asp?c=158&f=2421&t=299116&mc=139#934330
0 Kudos
TomJones
Emerging Contributor
Thanks for all your help. I'll try and implement it. 🙂
0 Kudos
TomJones
Emerging Contributor
Robert,

My project supervisor has decided to postpone the changes until the next phase of development, i.e. until we migrate to Flex 2.0.

Thanks for all your help and I'm sure you'll here from me then.

Regards
Tom :cool:
0 Kudos