How to get a layer in the map using layer name?

4173
11
Jump to solution
06-17-2019 12:30 PM
GarryStewart
New Contributor II

Hi, I'm working with the javascript 3.2 api, and have a map that loads from a pre-defined web map.   I'm now wanting to get a handle to a layer in the map using the layer name.  

I've managed to write some javacript code that locates the layerInfo for the said layer, but I have no idea how to get the actual layer from the map.

_getFeatureLayer: function(layerName) {     
var layer;
        
for (var j = 0, jl = this.map.layerIds.length; j < jl; j++)         
{             
   var currentLayerName = this.map.layerIds;             
   var currentLayer = this.map.getLayer(currentLayerName);               
   if (currentLayer.layerInfos.length > 0) 
   {                 
      layer = this._findSubLayerByName(currentLayer, layerName);                 
      if (layer != null) 
      {                     
         break;                 
      }             
   }         
}
return layer; 
},   

_findSubLayerByName: function (parentLayer, subLayerName) 
{     
   var layer;       
   for (var j = 0, jl = parentLayer.layerInfos.length; j < jl; j++)     
   {         
      var layerInfo = parentLayer.layerInfos;           
      if (layerInfo.name == subLayerName) 
      {             
         /* The correct layer is located, but how do I actually get the layer, this does not work. */
         var subLayerUrl = parentLayer.url + "/" + j;                          
         layer = new FeatureLayer(subLayerUrl); /* This doesn't give me the layer that is already added to the map */ 
         break;         
      }     
   }       
   return layer; 
}
0 Kudos
11 Replies
GarryStewart
New Contributor II

Hi Robert, can you use a selection geometry with a QueryTask?  Thanks,

0 Kudos
GarryStewart
New Contributor II

Nevermind, that was a dumb question … of course you can that's what a GIS is all about.