Map Legend and Layer Control for ESRI Flex API 2.0

Map Legend and Layer Control for ESRI Flex API 2.0

Rene Rubalcava | September 2, 2010

As GIS professionals when we make maps, one of the most critical components to that map has always been the legend. I have head the statement before that If your map needs a legend, there's something wrong with your cartography. Yeah right, maybe if you're making a street map, but when you are building analysis maps with varying symbology that could represent information contained in a thousand page report, making sure you italicize the labels of water features isn't going to cut it. When we try to represent our mapping information in a web application we can make every effort to simplify symbology, make our cartography as standard and clear as possible to users, but it is inevitable that someone will ask, hey, what's that dashed-line for? When ESRI released the Web ADF and we all built those cool, yet clunky web apps that had the map symbology built into the table of contents, we got spoiled. Web ADF is dead, and now you probably moved on to the ESRI Javascript API or the ESRI Flex API. But when you started using these API's you realized that one crucial component was missing, legend information. There were some tricky ways to get around this, even a REST service that pulled the legend information from the old school Web ADF SOAP service. Some just said screw it and resorted to taking screenshots of their ArcMap legend and working that into the application (we've all done it, no shame in fake it until you make it).

Recently, my twitter buddy ruprictGeek developed a Legend Resource Application using Sinatra that would generate an image of Legend for a map service using Ruby. This is a pretty awesome tool, and something that I think could prove useful for things like simplified metadata pages of available map services fr organizations, or bringing a quick legend into a web application. It also taught me a little Ruby and how if you are going to do some Ruby dev, you're better off not using Windows for it, just my opinion. But I digress, it got me started down a path to build something a little more robust for my Flex Applications.

I had put this implementation on the back burner when I realized how involved creating the symbology in the application would be. Then lo and behold a few weeks back, Mr. Robert Scheitlin released a Dynamic Legend Widget for FlexViewer 2.0. Robert has been a big contributor on the ESRI Flex forums and is a wizard of the ESRI Flex API and ESRI FlexViewer. Now, I don't use the FlexViewer for various reasons, but he did all of the leg work in his widget that was needed to actually translate the Map Service symbology into something that could be used in Flex. I already had a component that would pull Layers from a Map and allowed you to turn services on and off, as well as individual sub layers. I was pretty happy with it. But the one question I always get asked by my users is, Where's the legend? We can't release without a legend. As a side note, most developers might have a project with one or two core stakeholders that steer the project, my main project has about a dozen and if I have a dozen people asking for a legend, I listen.

So what I did was incorporate the work done by Mr. Scheitlin into my current Layer Control tool and created a LegendManager component that will display all symbology and allow you to turn layers on and off. I'm pretty happy with it. This component is part of my FlexMapTools library on github. I've included skins and itemRenderers in FlexMapTools, but feel free to modify these to meet your needs. Skin them up and make them as pretty as you'd like. You'll notice I have not built a skin for the button to expand sub layer details.

An example of the tool in action can be found here.

If you have some trouble using the component if you set up your visible layers using a config.xml file, please read the notes in LayerInfoDetails. You may need to search your visible layers using a string and not an int. It was an odd issue I ran into porting this tool to FlexMapTools.

As always, I am always looking for comments/critiques. I'm more intermediate, so I'm always looking to learn more and see how what Ive done stands up to the pros. Thanks again to Robert Scheitlin for tackling how to draw the legend symbology in Flex and thanks to Glenn Goodrich (ruprictGeek) for inspiring me to get this component off the ground.