Select to view content in your preferred language

Legend not displaying layer title text

6028
11
01-04-2013 09:12 AM
JimmyDobbins
New Contributor III
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
KellyHutchins
Esri Frequent Contributor
Hi Jimmy,

I just ran a quick test using the sample code but substituting your services and the layer title's show up in the legend. I took a quick look at your code and didn't see anything - perhaps there's something in your css that's hiding the legend titles? Here's my test code:

<!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.2/js/dojo/dijit/themes/claro/claro.css"> 
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/css/esri.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.2"></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 = [];
 
      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 princPortLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/3", {
            outFields: ["PORTNAME","PORTNUM"]
        });
        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"]
            });
        legendLayers.push({layer:schedKPortLayer, title:'Foreign Ports'});
                
                
                
        
        dojo.connect(map,'onLayersAddResult',function(results){
          var legend = new esri.dijit.Legend({
            map:map,
            layerInfos:legendLayers
          },"legendDiv");
          legend.startup();
        });
        
        map.addLayers([princPortLayer,schedKPortLayer]);

       dojo.connect(map,'onLayersAddResult',function(results){

        
        //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);
    </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="Ports" >
            <span style="padding:10px 0;">Click to toggle the visibilty of the various layers</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>




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
StevenHaslemore
Occasional Contributor
I've noticed the missing title issue too. I've found it will show up on subsequent refresh's of the legend.
0 Kudos