Select to view content in your preferred language

Handling of onLoad and onLayerAddResults differences and Legends

896
2
Jump to solution
10-25-2012 03:51 PM
ThomasHoman
Frequent Contributor
Hello all,

I've been trying to work through adding a legend to my app and have some confusion with the onLoad and onLayersAddResult event handlers. Right now the layers load but I never see a legend.

I'm currently loading the layers prior to the onLayersAddResult. Below is the init code.

Suggestions?

TIA

Tom

========================================================
      function init() {
        var initialExtent = new esri.geometry.Extent(
                 {"xmin":-12665450.01,"ymin":3882991.10,
                   "xmax":-11985726.92,"ymax":4106191.89,
                   "spatialReference":{"wkid":102100}});

var lods = [ //Zoom levels allowed
           {"level" : 9, "resolution" : 305.74811314055756, "scale" : 1155581.108577},
           {"level" : 10, "resolution" : 152.87405657041106, "scale" : 577790.554289},
           {"level" : 11, "resolution" : 76.43702828507324, "scale" : 288895.277144},
           {"level" : 12, "resolution" : 38.21851414253662, "scale" : 144447.638572},
           {"level" : 13, "resolution" : 19.10925707126831, "scale" : 72223.819286},
           {"level" : 14, "resolution" : 9.554628535634155, "scale" : 36111.909643},
           {"level" : 15, "resolution" : 4.77731426794937, "scale" : 18055.954822},
           {"level" : 16, "resolution" : 2.388657133974685, "scale" : 9027.977411},
           {"level" : 17, "resolution" : 1.1943285668550503, "scale" : 4513.988705},
           {"level" : 18, "resolution" : 0.5971642835598172, "scale" : 2256.994353},
           {"level" : 19, "resolution" : 0.29858214164761665, "scale" : 1128.497176}
     ];

        map = new esri.Map("mapDiv", {
          extent: initialExtent,
    lods: lods,
   logo: false
   });
    
//Load the elections Layer
        layer = new esri.layers.ArcGISDynamicMapServiceLayer(
            "http://gila-gis:6080/arcgis/rest/services/Elections/Elections2/MapServer");

        dojo.connect(map,'onLayersAddResult',function(results){
          var layerInfo = dojo.map(results, function(layer,index){
            return {layer:layer.layer,title:layer.layer.name};
          });
          if(layerInfo.length > 0){
            var legendDijit = new esri.dijit.Legend({
              map:map,
              layerInfos:layerInfo
            },"legendDiv");
            legendDijit.startup();
          }
        });

       map.addLayer(layer);

createBasemapGallery();
   

        dojo.connect(map, 'onLoad', function(map) {
            //resize the map when the browser resizes
            dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
    //after map loads, connect to listen to mouse move & drag events from coordinate tracking widget
            dojo.connect(map, "onMouseMove", showCoordinates);
            dojo.connect(map, "onMouseDrag", showCoordinates);
     buildLayerList(layer);  //Custom function to build layers into table prior to div placement
     //scalebar from scalebar widget
     var scalebar = new esri.dijit.Scalebar({
               map: map,
               scalebarUnit:'english'
  });
     });
    
      }
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor
Try switching the line where you add the layer to the map to the following:

map.addLayers([layer]);


The onLayersAddResult event only fires when you add layers to the map using map.addLayers. It doesn't work with map.addLayer.

View solution in original post

0 Kudos
2 Replies
KellyHutchins
Esri Frequent Contributor
Try switching the line where you add the layer to the map to the following:

map.addLayers([layer]);


The onLayersAddResult event only fires when you add layers to the map using map.addLayers. It doesn't work with map.addLayer.
0 Kudos
ThomasHoman
Frequent Contributor
Sweet! I knew I wasn't too far off but the last hurdle was a little too high.

Thanks again Kelly.

Tom
0 Kudos