Select to view content in your preferred language

How do I get a list of operational layers from a web map built with new Map() and map.addLayer?

3815
2
Jump to solution
09-02-2015 04:54 PM
ZianChoy
Frequent Contributor

I would like to use the new LayerList widget in version 3.14 of the SDK and I saw that the sample (Layer List Dijit ) calls an undocumented helper function to get a list of operational layers from a web map hosted by ArcGIS Online.

Is there any way to get a list of operational layers from a web map where the map and layers have been added using JavaScript calls so that I can pass the list to the LayerList widget? Right now, when I pass in the map object, I see things like "layer0" and "layer1".

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

Here's an example of how you can get a list of operational layers. In this snippet of code we add two layers (parks and trees) and listen to the layers-add-result event. Then we take the info on the layers from the event argument (response.layers) and pass that to the layer list widget. 

      var map = new Map("map",{
        center: [-118,34],
        zoom: 8,
        basemap: "streets"
      });
      
      var trees = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/ParkTrees/FeatureServer/0");
      map.addLayer(trees);
      var parks = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Parks/FeatureServer/0");
      map.on("layers-add-result", function(response){
             var myWidget = new LayerList({
             map: map,
             layers: response.layers
        },"layerList");
          myWidget.startup();
      });
      map.addLayers([parks, trees]);

View solution in original post

2 Replies
KellyHutchins
Esri Frequent Contributor

Here's an example of how you can get a list of operational layers. In this snippet of code we add two layers (parks and trees) and listen to the layers-add-result event. Then we take the info on the layers from the event argument (response.layers) and pass that to the layer list widget. 

      var map = new Map("map",{
        center: [-118,34],
        zoom: 8,
        basemap: "streets"
      });
      
      var trees = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/ParkTrees/FeatureServer/0");
      map.addLayer(trees);
      var parks = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Parks/FeatureServer/0");
      map.on("layers-add-result", function(response){
             var myWidget = new LayerList({
             map: map,
             layers: response.layers
        },"layerList");
          myWidget.startup();
      });
      map.addLayers([parks, trees]);
ZianChoy
Frequent Contributor

Thank you for the tip about using addLayers and the method's associated events. It worked out nicely.

0 Kudos