AnsweredAssumed Answered

Legend not displaying layer title text

Question asked by dobbinjp on Jan 4, 2013
Latest reply on Apr 28, 2017 by Sara_EL_MALKI
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) });
};

Outcomes