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".
Solved! Go to Solution.
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]);
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]);
Thank you for the tip about using addLayers and the method's associated events. It worked out nicely.