AnsweredAssumed Answered

Legend widget works but won't display layers until zoom in or out

Question asked by lorieme on Jul 13, 2017
Latest reply on Jul 14, 2017 by rscheitlin

I am able to create the Legend using the Legend Dijit in ArcGIS JavaScript API 3.21, but the layers that are set to visible won't display in the legend until I zoom in or zoom out or turn on another layer.  What causes this?  Note:  the LegendDiv is assigned a value of "No Legend" from the Legend Dijit when the page first loads, rather than any layers.


In the map services, none of the layers are turned on.  In the JavaScript, I set some of the layers to visible.  I have even checked that the layers are indeed visible by the time the legend is being built.


I have three map services so I set up the legend like this (partial code):


var pointsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(pointsURL, {

id: "pointsLayer",

visible: true


var linesLayer = new esri.layers.ArcGISDynamicMapServiceLayer(linesURL, {

id: "linesLayer",

visible: true


var polygonsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(polygonsURL, {

id: "polygonsLayer",

visible: true



map = new Map("map", {

extent: customExtentAndSR


//home button

var home = new HomeButton({

map: map

}, "HomeButton");



var scalebar = new Scalebar({

map: map



map.on("load", function () {

toolbar = new Draw(map);

toolbar.on("draw-end", addToMap);

//toolbar.on("draw-complete", addMeas);

editToolbar = new Edit(map);




map.on("click", function (evt) {









// set up identify task click event

connects = on(map, "click", executeIdentifyTask);



map.on("layers-add-result", function () {

// test to see if sub-layers have been set as visible

for (var j = 0; j < map.layerIds.length; j++) {

var layer = map.getLayer(map.layerIds[j]);

alert( + ' ' + layer.opacity + ' ' + layer.visible + ' ' + layer.visibleLayers);


// add legend

legendDijit = new Legend({

map: map,

layerInfos: [{

layer: polygonsLayer,

hideLayers: [indexPoly_CountyBdry, indexPoly_MuniBdry],

title: " ",

respectCurrentMapScale: true



layer: linesLayer,

hideLayers: [indexLine_PriorityArray],

title: " ",

respectCurrentMapScale: true



layer: pointsLayer,

hideLayers: [indexPoint_ControlPts, indexPoint_FiberPts, indexPoint_ReferencePts, indexPoint_SchoolBusStopsOSPI],

title: " ",

respectCurrentMapScale: true


}, "legendDiv");





map.addLayers([aerialLayer, aerialLayerZoom, tilesLayer, polygonsLayer, linesLayer, pointsLayer]);