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");
home.startup();
//scalebar
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);
pointsLayer.setVisibleLayers([myPointsArray]);
linesLayer.setVisibleLayers([myLinesArray]);
polygonsLayer.setVisibleLayers([myPolygonsArray]);
map.on("click", function (evt) {
editToolbar.deactivate();
});
createMapMenu();
createGraphicsMenu();
map.reposition();
map.resize();
buildLayerList();
firstPrint();
// 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
alert(layer.id + ' ' + 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");
legendDijit.startup();
});
map.addLayers([aerialLayer, aerialLayerZoom, tilesLayer, polygonsLayer, linesLayer, pointsLayer]);
Lori,
Have you tried adding legendDijit.refresh(); after the startup?
Robert,
Yes, and still the same results.
I changed the the API version from 3.21 to 3.16 (I also tried 3.20, and 3.18). Using API version 3.16 is what fixed the problem.
Strange I can't explain that one.