norcalheidi did you ever get an answer to your problem? I have been beating my head over this for both the Silverlight API and the JS API.
Is there anyway to add the legend symbol to the list of check boxes? I would like to just have the check boxes so that user does not have to switch back and forth. The API reference does not have anything in it on this. Does anyone know of a good work around?ThanksExample[ATTACH=CONFIG]16448[/ATTACH]
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
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>/legende.g. http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/legendDoes yours legend show up at the REST endpoint?
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.
<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>
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?
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. 🙂
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.
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.
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.
Shuping,Thanks for the code. It does as you say and adds checkboxes for all sublayers. But did you notice that the legend is tripled? It puts the same layers in the legend three times in a row. I think this is a step in the right direction regarding the checkboxes though.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> <!--The viewport meta tag is used to improve the presentation and behavior of the samples on iOS devices--> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/> <title>Updating the legend to display visible layers</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.css"> <style> html, body { height: 98%; width: 98%; margin: 0; padding: 5px; } #rightPane{ width:20%; } #legendPane{ border: solid #97DCF2 1px; } </style> <script type="text/javascript">var djConfig = {parseOnLoad: true};</script> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0"></script> <script type="text/javascript"> dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("dijit.layout.AccordionContainer"); dojo.require("esri.map"); dojo.require("esri.dijit.Legend"); dojo.require("esri.arcgis.utils"); dojo.require("dijit.form.CheckBox"); var map; var legendLayers = []; var legendLayer; var legend; function init() { var initialExtent = new esri.geometry.Extent({"xmin":-117.98,"ymin":33.95,"xmax":-116.92,"ymax":34.44,"spatialReference":{"wkid":4326}}); map = new esri.Map("map", { extent: esri.geometry.geographicToWebMercator(initialExtent)}); //Add the terrain service to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"); map.addLayer(basemap); var quakeLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer",{id:'quakes'}); legendLayers.push({layer:quakeLayer,title:'Earthquakes'}); var fireLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer",{id:'fire'}); legendLayers.push({layer:fireLayer,title:"Fire"}); dojo.connect(map,'onLayersAddResult',function(results){ legend = new esri.dijit.Legend({ map:map, layerInfos:legendLayers },"legendDiv"); legend.startup(); }); map.addLayers([fireLayer,quakeLayer]); dojo.connect(map,'onLayersAddResult',function(results){ var layerLists=[]; dojo.forEach(legendLayers,function(layer){ layerLists.push(buildLayerList(layer)); }); dojo.byId("layer_list").innerHTML = layerLists.join(''); }); dojo.connect(map, 'onLoad', function(theMap) { //resize the map when the browser resizes dojo.connect(dijit.byId('map'), 'resize', map,map.resize); }); } function buildLayerList(layer) { var visible = []; var hide=[]; var items = dojo.map(layer.layer.layerInfos,function(info,index){ if (info.defaultVisibility) visible.push(info.id); else hide.push(input.id); return "<input type='checkbox' class='" + layer.title + "' checked='true' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label><br>"; }); layer.layer.setVisibleLayers(visible); legend.hidLayers=hide; legend.refresh(); return items.join(' '); } function updateLayerVisibility() { dojo.forEach(legendLayers,function(layer){ var inputs = dojo.query("."+ layer.title), input; var visible = []; var hide=[]; dojo.forEach(inputs,function(input){ if (input.checked) visible.push(input.id); else hide.push(input.id); }); //if there aren't any layers visible set the array to be -1 if(visible.length === 0){ visible.push(-1); } layer.layer.setVisibleLayers(visible); legend.hidLayers=hide; legend.refresh(); }); } dojo.addOnLoad(init); </script> </head> <body class="claro"> <div id="content" dojotype="dijit.layout.BorderContainer" design="headline" gutters="true" style="width: 100%; height: 100%; margin: 0;"> <div id="rightPane" dojotype="dijit.layout.ContentPane" region="right"> <div dojoType="dijit.layout.AccordionContainer"> <div dojoType="dijit.layout.ContentPane" id="legendPane" title="Legend" selected="true"> <div id="legendDiv"></div> </div> <div dojoType="dijit.layout.ContentPane" title="Natural Disasters" > <span style="padding:10px 0;" id="layer_list"></span> <div id="toggle" style="padding: 2px 2px;"></div> </div> </div> </div> <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;"> </div> </div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> <!--The viewport meta tag is used to improve the presentation and behavior of the samples on iOS devices--> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/> <title>Updating the legend to display visible layers</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.css"> <style> html, body { height: 98%; width: 98%; margin: 0; padding: 5px; } #rightPane{ width:20%; } #legendPane{ border: solid #97DCF2 1px; } </style> <script type="text/javascript">var djConfig = {parseOnLoad: true};</script> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0"></script> <script type="text/javascript"> dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("dijit.layout.AccordionContainer"); dojo.require("esri.map"); dojo.require("esri.dijit.Legend"); dojo.require("esri.arcgis.utils"); dojo.require("dijit.form.CheckBox"); var map; var legendLayers = []; var legendLayer; function init() { var initialExtent = new esri.geometry.Extent({"xmin":-117.98,"ymin":33.95,"xmax":-116.92,"ymax":34.44,"spatialReference":{"wkid":4326}}); map = new esri.Map("map", { extent: esri.geometry.geographicToWebMercator(initialExtent)}); //Add the terrain service to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"); map.addLayer(basemap); var quakeLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer",{id:'quakes'}); legendLayers.push({layer:quakeLayer,title:'Earthquakes'}); var fireLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer",{id:'fire'}); legendLayers.push({layer:fireLayer,title:"Fire"}); dojo.connect(map,'onLayersAddResult',function(results){ var legend = new esri.dijit.Legend({ map:map, layerInfos:legendLayers },"legendDiv"); legend.startup(); }); map.addLayers([fireLayer,quakeLayer]); dojo.connect(map,'onLayersAddResult',function(results){ var layerLists=[]; dojo.forEach(legendLayers,function(layer){ layerLists.push(buildLayerList(layer)); }); dojo.byId("layer_list").innerHTML = layerLists.join(''); }); dojo.connect(map, 'onLoad', function(theMap) { //resize the map when the browser resizes dojo.connect(dijit.byId('map'), 'resize', map,map.resize); }); } function buildLayerList(layer) { var visible = []; var items = dojo.map(layer.layer.layerInfos,function(info,index){ if (info.defaultVisibility) { visible.push(info.id); } return "<input type='checkbox' class='" + layer.title + "' checked='true' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label><br>"; }); layer.layer.setVisibleLayers(visible); return items.join(' '); } function updateLayerVisibility() { dojo.forEach(legendLayers,function(layer){ var inputs = dojo.query("."+ layer.title), input; var visible = []; dojo.forEach(inputs,function(input){ if (input.checked) { visible.push(input.id); } }); //if there aren't any layers visible set the array to be -1 if(visible.length === 0){ visible.push(-1); } layer.layer.setVisibleLayers(visible); }); } dojo.addOnLoad(init); </script> </head> <body class="claro"> <div id="content" dojotype="dijit.layout.BorderContainer" design="headline" gutters="true" style="width: 100%; height: 100%; margin: 0;"> <div id="rightPane" dojotype="dijit.layout.ContentPane" region="right"> <div dojoType="dijit.layout.AccordionContainer"> <div dojoType="dijit.layout.ContentPane" id="legendPane" title="Legend" selected="true"> <div id="legendDiv"></div> </div> <div dojoType="dijit.layout.ContentPane" title="Natural Disasters" > <span style="padding:10px 0;" id="layer_list"></span> <div id="toggle" style="padding: 2px 2px;"></div> </div> </div> </div> <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;"> </div> </div> </body> </html>
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.