2

New Code: I updated the code a bit to more a recent recent version of the api, but ashima was correct in that I was not passing my layer as an array. Fixed that and it worked

 map = new Map("map", {                     
   basemap: "streets",
   center: [ -84.32, 46.5], // long, lat
    zoom: 13
});             

//fetches myMap layer
var myMap = new FeatureLayer(myMapLocation, {
  mode: FeatureLayer.MODE_ONDEMAND,                     
  outFields:["*"]
});

//add the legend
map.on("layers-add-result", function (evt) {                    
    var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
    return {layer:layer.layer, title:layer.layer.name};
});                         
if (layerInfo.length > 0) {                 
    var legendDijit = new Legend({
        map: map,
        layerInfos: layerInfo
    }, "legend");
legendDijit.startup();
    }
});

map.addLayers([myMap]);

So I've been using the ESRI Javascript API, so far what I've been able to do is create a map, fetch a layer from another source and combine them, but I'm stuck on grabbing the legend from the other source and also adding that. This is what I currently have after my attempts (also tried switching the map : map to map : mymap) The myMapLocation is just set to "/MapServer" and not "/mapServer/layers" or anything

 map = new Map("mapDiv", {
                    basemap: "streets"                      
                });
myMap = new esri.layers.ArcGISDynamicMapServiceLayer(myMapLocation);

map.on('layers-add-result', function () {
//this section breaks v              
    var legend = new Legend({
        map: map,
        layerInfos: legendLayers
    }, "legendDiv");
// ^
    legend.startup();
});

legendLayers.push({ layer: myMap });

map.addLayers([myMap]);
2
  • ashima is correct that the constructor option expects an array. your use of a variable named 'myMap' to refer to your DynamicMapServiceLayer and 'myMapLocation' to refer to the url of the map service itself makes the code a little difficult to comprehend. Commented Oct 21, 2014 at 21:21
  • @demiskeleton did you define arrayUtils somewhere? The console keeps giving me an "undefined" error. Also, what version of the API did you use?
    – daniella
    Commented Mar 26, 2015 at 23:40

1 Answer 1

2

It looks like you have not declared legendLayers as an array (var legendLayers = [];) before using it. If that doesn't solve your problem, could you paste the error that you get when you run this code in the browser?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.