Legend with Visible Layers - how to modify for sublayers?

10473
28
07-12-2012 04:55 AM
CurtWalker
New Contributor
I'm using this tutorial to create a map with legend and checkboxes for turning layers on and off.  Currently it only gives one checkbox per map service if you are using a Dynamic Map Service (which I am because I need labels and Feature Layers do not support labels).

How can I modify this code so it gives a checkbox for every sublayer in the Dynamic Map Service instead of just one checkbox that turns everything on/off?
0 Kudos
28 Replies
SiqiLi
by Esri Contributor
Esri Contributor
That's really weird. I just tried your code again to make sure.  I pasted your code into a fresh document and hit it with IE and FF.  In FF the legend appears normal, but in IE9 I get the tripled legend. 


Umm....My IE9 works fine with it. Try to clear the browser cache to see if it helps.


Hey Curt,
Update: I found tripled legend issue is reproducible in 32-bit IE. The issue is caused by the unnecessary call of "legend.refresh();" in the "buildLayerList()" function. Go ahead and remove it. The app shall runs correctly this time.

[ATTACH=CONFIG]16065[/ATTACH]
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
To change the visibility of each individual layer in the map, you need to work with the   ArcGISDynamicMapServiceLayer.setVisibleLayers() method. 

To change the visibility of each individual layer in the legend control, you need to work with the   Legend.hideLayers property. Then follow by calling   Legend.fefresh() method. 


Update: I tested again and found there seems no need to use the Legend.hideLayers property. The legend control knows which layer has changed its visibility. Attached is the updated sample.

Correct: In the #6 post, code lines #92 and #113 have spell errors. They should be "legend.hideLayers=hide;". I missed the "e". Sorry. 🙂
0 Kudos
PramodHarithsa1
Occasional Contributor II
Update: I tested again and found there seems no need to use the Legend.hideLayers property. The legend control knows which layer has changed its visibility. Attached is the updated sample.  

Correct: In the #6 post, code lines #92 and #113 have spell errors. They should be "legend.hid  eLayers=hide;". I missed the "  e". Sorry. 🙂


Hi Shuping,

The code works fine with the esri sample server, but when i replace it with my map service neither the TOC nor the legend gets created.
I am interested only in TOC though.
Am i missing anything here?
0 Kudos
CurtWalker
New Contributor
Update: I tested again and found there seems no need to use the Legend.hideLayers property. The legend control knows which layer has changed its visibility. Attached is the updated sample.  

Correct: In the #6 post, code lines #92 and #113 have spell errors. They should be "legend.hid  eLayers=hide;". I missed the "  e". Sorry. 🙂


Thanks, that did fix the triple legend issue for me. But, when I have pulled in my own map service it does not show up in the table of contents with checkboxes. It does however appear in the legend. I"m not sure what's keeping it from working in the TOC.
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
Hi Shuping,

The code works fine with the esri sample server, but when i replace it with my map service neither the TOC nor the legend gets created.
I am interested only in TOC though.
Am i missing anything here?



Are you working with ArcGIS 10.0 or newer map service? If it is a 10.0 SP1 or newer map service, you can check its legend resource directly at the REST endpoint via URL: http://<mapservice-url>/legend

e.g. http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/...

Does yours legend show up at the REST endpoint?
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
Thanks, that did fix the triple legend issue for me.  But, when I have pulled in my own map service it does not show up in the table of contents with checkboxes.  It does however appear in the legend.  I"m not sure what's keeping it from working in the TOC.


I add the checkboxes to the "dojo.byId("layer_list").innerHTML". So, it depends on where the "layer_list" element is defined in the HTML, in the TOC or Legend ContentPane.

<div dojoType="dijit.layout.ContentPane" title="TOC" >
            <span style="padding:10px 0;" id="layer_list"></span>
            <div id="toggle" style="padding: 2px 2px;"></div>
          </div>
0 Kudos
CurtWalker
New Contributor
Are you working with ArcGIS 10.0 or newer map service? If it is a 10.0 SP1 or newer map service, you can check its legend resource directly at the REST endpoint via URL: http://<mapservice-url>/legend

e.g. http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/...

Does yours legend show up at the REST endpoint?


Yes it sure does.

https://map.entrix.com/ArcGIS/rest/services/07856001/titan_working_20111220_acw/MapServer/legend
0 Kudos
CurtWalker
New Contributor
I don't understand it.  If I copy and paste your code into a fresh document and hit it in a browser, I get a webmap with the ESRI layers and both Legend and TOC work just fine.  Then the only thing I change is to paste the URL for my map service in place of the quakes layer, and refresh.  The map loads, the data displays, and the legend populates, but the checkboxes for the TOC are gone.  It won't even show the checkboxes for the other ESRI sublayers.  What is it about my map service that breaks the TOC?

http://map.entrix.com/ArcGIS/rest/services/07856001/titan_working_20111220_acw/MapServer
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
I don't understand it.  If I copy and paste your code into a fresh document and hit it in a browser, I get a webmap with the ESRI layers and both Legend and TOC work just fine.  Then the only thing I change is to paste the URL for my map service in place of the quakes layer, and refresh.  The map loads, the data displays, and the legend populates, but the checkboxes for the TOC are gone.  It won't even show the checkboxes for the other ESRI sublayers.  What is it about my map service that breaks the TOC?

http://map.entrix.com/ArcGIS/rest/services/07856001/titan_working_20111220_acw/MapServer


Try this one:
0 Kudos
CurtWalker
New Contributor
Shuping,

Thanks for the new code.  The legend is now working with my service, thanks.  However, when the map loads only two layers are visible on the map and in the legend, yet all of the checkboxes in the TOC are checked.  If I uncheck one of them it fires the functions and suddenly all the other layers appear all at once, both in the map and in the legend.

It's very close!  Just this last bug and it'll will be perfect.  Thanks for your help.  Any ideas?
0 Kudos