Select to view content in your preferred language

Legend not displaying layer title text

6167
11
01-04-2013 09:12 AM
JimmyDobbins
Occasional Contributor
Hello,

I am following this example to display a legend with checkboxes to show/hide the layers.

When I load the page, the layer icons are visible, but the layer titles do not show up. Note that I am working with featurelayers and not arcgisdynamicservicelayers like in the example.

I am attaching my map.js file. Please let me know if you see anything or have any guidance to get the titles to show up.

Thanks in advance, I appreciate the help!

dojo.require("dijit.dijit");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("esri.map");
dojo.require("esri.dijit.OverviewMap");
dojo.require("esri.dijit.Scalebar");
dojo.require("esri.dijit.BasemapGallery");
dojo.require("esri.layers.FeatureLayer");
dojo.require("esri.dijit.InfoWindow");
dojo.require("esri.dijit.Legend");
dojo.require("esri.arcgis.utils");
dojo.require("dijit.form.CheckBox");

var map;
var legendLayers = [];

function init() {
    //calculate the extent for the initial zoom
    var initExtent = new esri.geometry.Extent({"xmin":-15375461.113615725,"ymin":2593049.7475456917,"xmax":-6066042.564710014,"ymax":6413678.169350926,"spatialReference":{"wkid":102100}});

    //code to create the map and add a basemap
    map = new esri.Map("mapDiv", {
        extent: initExtent
    }
    );

    var basemapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
    var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapURL);
    map.addLayer(basemap);
  
 //resize the infowindow
 //map.infoWindow.resize(150,300);
 
 //infotemplate code creation here
 var pportContent = "<table><tr><td><b>Port Code:</b></td><td style='text-align:right'>${PORTNUM}</td></tr></table>";

    // the code below can be included to pass a parameter to a web service, asp.net page, etc.
    // + "<a href='http://www.vanderbilt.edu/vector&portcode=${PORT}'>Click here to view historical data</a>";
 
 var schedKContent = "<table><tr><td><b>Port Code:</b></td><td style='text-align:right'>${SCHEDK}</td></tr>"
                 + "<tr><td><b>Country Name:</b></td><td style='text-align:right'>${CTRYNAME}</td></tr></table>";
    
 //infotemplates for principal and schedule K ports
 var infotemplatePports = new esri.InfoTemplate("${PORTNAME}",pportContent);
 var schedKtemplatePorts = new esri.InfoTemplate("${FPRTNAME}",schedKContent);
 
    dojo.connect(map, 'onLoad', function (map) {
        var scalebar = new esri.dijit.Scalebar({
            map: map
        });

        //add the overview map 
        overviewMapDijit = new esri.dijit.OverviewMap({
            map: map
        }, dojo.byId('overviewMapDiv'));
        overviewMapDijit.startup();
        createBasemapGallery();
    });

    var princPortLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/3", {
     outFields: ["PORTNAME","PORTNUM"],
     infoTemplate: infotemplatePports
    });
    legendLayers.push({ layer: princPortLayer, title: 'U.S. Ports' });
   
    var schedKPortLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/2", {
     outFields: ["SCHEDK","FPRTNAME","CTRYNAME"],
     infoTemplate: schedKtemplatePorts
     });
    legendLayers.push({layer:schedKPortLayer, title:'Foreign Ports'});
  
  map.addLayers([princPortLayer, schedKPortLayer]);
  
  dojo.connect(map,'onLayersAddResult',function(results){

        var legend = new esri.dijit.Legend({
            map: map,
            layerInfos: legendLayers.title
        }, "legendDiv");
        legend.startup();
  
        //add check boxes 
        dojo.forEach(legendLayers,function(layer){         
         var layerName = layer.title;
         var checkBox = new dijit.form.CheckBox({
            name: "checkBox" + layer.layer.id,
            value: layer.layer.id,
            checked: layer.layer.visible,
            onChange: function(evt) {
              var clayer = map.getLayer(this.value);
              clayer.setVisibility(!clayer.visible);
              this.checked = clayer.visible;
            }
          });

          //add the check box and label to the toc
          dojo.place(checkBox.domNode,dojo.byId("toggle"),"after");
          var checkLabel = dojo.create('label',{'for':checkBox.name, innerHTML:layerName},checkBox.domNode,"after");
           dojo.place("<br />",checkLabel,"after");
        });

        });
       
     dojo.connect(map, 'onLoad', function(theMap) {
          //resize the map when the browser resizes
          dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
        });
      }
  
      dojo.addOnLoad(init);
  
function createBasemapGallery() {
    //add the basemap gallery to display maps from ArcGIS.com including bing maps
    var basemapGallery = new esri.dijit.BasemapGallery({
        showArcGISBasemaps: true,
        map: map
    }, "basemapGalleryDiv");
    basemapGallery.startup();
    dojo.connect(basemapGallery, "onError", function (msg) { console.log(msg) });
};
0 Kudos
11 Replies
JimmyDobbins
Occasional Contributor
Here is my html:

[HTML]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>VECTOR - Western Hemisphere Maritime Cargo Data System (2.0)</title>
    <link href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/claro/claro.css" rel="stylesheet" type="text/css" />
    <!--  other available themes follow
    <link href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/tundra/tundra.css" rel="stylesheet" type="text/css" />
    <link href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/soria/soria.css" rel="stylesheet" type="text/css" />
    <link href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/nihilo/nihilo.css" rel="stylesheet" type="text/css" />
    -->
    <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/css/esri.css" />
    <link href="css/StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        djConfig = { parseOnLoad: true };
    </script>
    <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.2" type="text/javascript"></script>
    <script src="js/map.js" type="text/javascript"></script>
</head>
<body class="claro">
    <script src="http://transp40.vuse.vanderbilt.edu/js/vectorbrandbar.js" type="text/javascript"></script>
      <div id="mainDiv" dojotype="dijit.layout.BorderContainer">
   <div id="headerDiv" dojotype="dijit.layout.ContentPane" region="top"><h3>Western Hemisphere Maritime Cargo Data System (2.0)</h3></div>
            <div id="rightBorderDiv" dojotype="dijit.layout.BorderContainer" region="right">
                <div id="rightDiv" dojotype="dijit.layout.ContentPane" region="top">right pane</div>
                <div id="rightBottomDiv" dojotype="dijit.layout.ContentPane" height="100%" region="bottom">
                    <div id="overviewMapDiv" style="width: 100%; height: 100%"></div>
                </div>
            </div>
      <div id="mapDiv" class="shadow" dojotype="dijit.layout.ContentPane" region="center"></div>
   <div id="leftDiv" dojotype="dijit.layout.ContentPane" region="left">
    <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="Toggle Layers">
      <div id="toggle" style="padding: 2px 2px;"></div>
     </div>
     <div dojotype="dijit.layout.ContentPane" title="Base Maps">
                    <div id="basemapGalleryDiv"></div>
                    </div>
    </div>  
   </div>
   <div id="footerDiv" dojotype="dijit.layout.ContentPane" region="bottom">footer</div>
  </div>
</body>
</html>
[/HTML]
0 Kudos
JimmyDobbins
Occasional Contributor
Some more information that may prove helpful...I want to clarify that the checkboxes functionality is working correctly. I am attaching a screenshot showing the checkboxes and the legend icons (without the layer title).
[ATTACH=CONFIG]20447[/ATTACH]
[ATTACH=CONFIG]20448[/ATTACH]
0 Kudos
DavidMcGill
Deactivated User
I have an 'id' and a  "title" defined in my dynmap

var dynmap2 = new esri.layers.ArcGISDynamicMapServiceLayer("http://xxx.xx.xxx.xx/arcgis/rest/services/xxxxx/xxxx_PipeDistributionSys/MapServer", { opacity: 0.3, id: 'Pipe Distribution' });
            legendLayers.push({ layer: dynmap2, title: ' ' });




then change some of your code. 

var legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv");
                legend.startup();
            });
0 Kudos
JimmyDobbins
Occasional Contributor
Thanks for the response. I am wondering if featurelayers don't work the same way the arcgisdynamiclayers work. I modified my code by adding an id field to each layer and I get the same result (no layer title in the legend). Let me know if you see anything I have wrong. Thanks again!

A sample is below:

var portseriesLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/4", {
     outFields: ["NAV_UNIT_N","CITY_OR_TO","STATE_POST","OPERATORS","PURPOSE","COMMODITIE"],
     infoTemplate: infotemplatePortSeries,
 id: "Water Terminals"
     });
  legendLayers.push({layer:portseriesLayer,title:"Water Terminals"});

//add legend icons
  dojo.connect(map,'onLayersAddResult',function(results){

        var legend = new esri.dijit.Legend({
            map: map,
            layerInfos: legendLayers
        }, "legendDiv");
        legend.startup();
  });
JakeSkinner
Esri Esteemed Contributor
Try specifying each layer and title in the 'layerInfos' for the legend constructor:

var legend = new esri.dijit.Legend({
            map: map,
            layerInfos:[{layer:princPortLayer, title:"U.S. Ports"}, 
            {layer:schedKPortLayer, title:"Foreign Ports"}]
        }, "legendDiv");
        legend.startup();
SaraEL_MALKI
Frequent Contributor

after many years, your answer has helped me, thanks mister Jake.

0 Kudos
JimmyDobbins
Occasional Contributor
Thanks for the suggestion, Jake. I thought that was going to work, but I got the same result (no title showing up).

Starting to look like a hard-coded static image is in my future...
0 Kudos
AdrianMarsden
Honored Contributor
HiI raised a similar issue here http://forums.arcgis.com/threads/72109-print-widget-task-default-legend-for-templateIt looked like it was a setting in the original mxdAway from work at present so can't test other solutions here, but I am guessing they may fail for me too. CheersACM
0 Kudos
AdrianMarsden
Honored Contributor
Sorry editing on mobile site seems to have lost line breaks
0 Kudos