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

4172
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
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
11 Replies
RobertScheitlin__GISP
MVP Emeritus

Garry,

  You should return your feature layer inside your for loop:

_findSubLayerByName: function (parentLayer, subLayerName) 
{     
   var layer;       
   for (var j = 0, jl = parentLayer.layerInfos.length; j < jl; j++)     
   {         
      var layerInfo = parentLayer.layerInfos[j];           
      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 */ 
         return layer;
      }     
   }
}
0 Kudos
GarryStewart
New Contributor II

Hi Robert, 

I appreciate your input, however that doesn't answer why I am not getting the feature layer that has already been added to the map.  The break statement causes the code to exit the loop and the return at the end does return the "new" feature layer, but not the one that's currently in the map.

The return statement is the last thing to happen in my code, as it is best practice in programming to have only one exit point from a function.  

Any other thoughts on how to get a layer that is in the map using layer name or layerInfo for that matter?

Thanks so much.

Garry

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Garry,

   Are you saying that you have a Feature Layer added to your map already that would be the same as the sublayer of the ArcGISDynamicMapServiceLayer you are searching in?... Just cause you have a ArcGISDynamicMapServiceLayer added to the map does not mean that there is a FeatureLayer created in the map for each of it's sublayers.

0 Kudos
GarryStewart
New Contributor II

Hi Robert, again thank you so much for your fast reply.

We are serving a web map through portal, and so the web app builder is using the portal url and web map id to load the map into the web application (map viewer).   

I can access individual feature layers on the map using the url and adding the id number of the layer ...

https://gisserver.domain.com/secure/rest/services/MyCustomMap/MapServer/0

I know this because I previously used the LocalLayers widget to explicitly load individual FeatureLayers from the map service using the above url syntax... and my map loaded fine and we were able to interact with the feature layers.

However, I don't want to have to predefine my layers in configuration.  I simply want the map in portal to open in my web application.  And then be able to find a layer in that loaded map using the layer name.

Was hoping for something simple like this.map.getLayer("LayerNameHere") but it seems this gets only the first level layers.   Anything nested requires a recursive loop like I do in my code.   Or at least that's what I think is required.

Thanks again, I'm pretty green with this ..

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Garry,

   OK. Let me break in down for you then. When you add a url like https://gisserver.domain.com/secure/rest/services/MyCustomMap/MapServer to your web map that layer i...

GarryStewart
New Contributor II

Thank you Robert, I appreciate your patience and expertise.   It sounds like we may have to load an initial map, and then add explicit FeatureLayers to the loaded map, so we can interact with them performing feature selection and highlighting, etc.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Garry,

   No not really. You can just use the sublayer url like you were getting and use a QueryTask to query that layer and add a GraphicsLayer to the map to show the selection/highlight of features from that layer.

0 Kudos
GarryStewart
New Contributor II

Hi Robert.  I took a look at the code and see it is using the jimu.js SelectionManager.js class, which uses a the geometry engine and a feature layer to locate features, and then as you said adds a graphics layer for the selection results.   So yes, we should be able to just new a FeatureLayer on the fly with the url, and run the select which should add a graphics layer on top of the map image and still give us access to individual features if needed.   I'm pretty green to this, so I apologize if I'm rambling nonsense.   I appreciate your assistance greatly.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Garry,

   But the use of a FeatureLayer and/or SelectionManager.js is not needed either. Once you have your sublayer url you can execute a queryTask on that url and never have to use a FeatureLayer(FL), since the results of a QueryTask can return the queried features geometry and you can add that geometry to a GraphicsLayer(GL) and add that GL to your map and now you have highlighted features in the map and not had to use a FL at all.

0 Kudos