Select to view content in your preferred language

Iterating over the Layers in a service

475
1
09-09-2011 12:45 PM
BrianGustafson
Occasional Contributor
I want to do a foreach loop over the layers in a service then test to see if the name matches the name that I passed in.  I am doing this to get the layer ids for the identify task.  I used a similar function in silverlight but I cannot convert it to java script.  Any ideas?

function GetLayerId(LayerName)
        {
            var i = -1;
            var lyr = new esri.layers.ArcGISDynamicMapServiceLayer("...")

            lyr.layerInfos.forEach(
                function x(value) {
                    alert(value);
                    if (value.name.toUpper() == LayerName.toUpper()) {
                        i = value.id;
                    }
                }
            );

            return i;
        }
0 Kudos
1 Reply
SiqiLi
by Esri Contributor
Esri Contributor
Here is a sample for you:
 var map = new esri.Map("map");
        var basemap = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");
  
     dojo.connect(basemap, 'onLoad', function(theMap) {        
          dojo.map(theMap.layerInfos,function(info,index){
         if(info.name == "Rivers")
          alert("Rivers layer id is: " +  info.id);
           });
        });
0 Kudos