How to identify layer type in a web map

712
5
Jump to solution
07-13-2018 03:02 PM
JacksonTrappett
Occasional Contributor II

I have many web maps on Portal that have several web layers in each.  They include dynamic services which are "map image layers" on portal, vector tile layers, and a cached basemap.  I'm writing some code (API 3.24) that will run an identify task on only the dynamic layers (map image layers) in the map, and I want to ignore the rest of the layers.  I used this code:

var webMapLayers = arrayUtils.map(this.map.layerIds, lang.hitch(this, function (layerId) {
      return this.map.getLayer(layerId);
}));

to get a list of the layers in the map.  If I log those on the console like this:

webMapLayers.forEach(function(element) { console.log(element); });

I see this in the browser console:

{_attrs: {…}, url: "https://myserver/arcgis/rest/services/BaseMap/BaseImage/MapServer", _url: {…}, spatialReference: {…}, initialExtent: {…}, …}
{_attrs: {…}, url: "https://myserver/arcgis/rest/services/Hosted/MAPvectorTiles/VectorTileServer", _url: {…}, spatialReference: {…}, initialExtent: {…}, …}
{_attrs: {…}, url: "https://myserver/arcgis/rest/services…Tiles/VectorTileServer/resources/styles/root.json", _url: {…}, spatialReference: {…}, initialExtent: {…}, …}
{_attrs: {…}, url: "https://myserver/arcgis/rest/services/TownMap_MIL1/MapServer", _url: {…}, spatialReference: {…}, initialExtent: {…}, …}

I can expand these and see hundreds, if not thousands of properties on each of these layers.  I was hoping to find one property in there somewhere that would let me determine which layer(s) are "map image layers" or dynamic layers, so I can only use those layers for the identify Task, but after spending quite a bit of time combing through, and also looking at the API documentation, I can't find any common property between those four layer types that lets me tell them apart.  I can of course manually tell which service is which by the service names, but I have a lot of webmaps all with different combinations of layers, and I want to write this code in a generic way so that no matter what is in the map, it will only pick out the dynamic (map image) layers.

Does anyone know an easy way to do this?

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Jackson,

   Sorry for the delay.

        arcgisUtils.createMap("4abe6a830b8f466dacf8abfde567a781", "map").then(function (response) {
          var legendLayers = arcgisUtils.getLegendLayers(response);
          legendLayers.forEach(function(element) {
            console.info(element.layer.declaredClass);
          });
        });

You can use declaredClass for that

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Jackson,


   It should be as simple as if(typeof(element) === ArcGISDynamicMapServiceLayer) {

  //do something

}

JacksonTrappett
Occasional Contributor II

Thank you Robert!  I gave this a try and it doesn't seem to be working for me:

var webMapLayers = arrayUtils.map(this.map.layerIds, lang.hitch(this, function (layerId) {
   return this.map.getLayer(layerId);
}));

webMapLayers.forEach(function(element)
{
console.log(element.id); //log 'id' outside if to show which layers we have
if(typeof(element) === ArcGISDynamicMapServiceLayer)
{
console.log(element.url); //log 'url' inside the loop to show which ones met the criteria
}
});

and the results are:

GJBaseImage_5386
17a8da996e40453e88d220cc7e59a462
VectorTile_3761
c44870356b774668891921a4aa44908f

The first console.log returned 4 layer ids, outside the if statement, but the console.log inside the if statement didn't return anything because none of the layers met the criteria, even though the layer with the ID

"c44870356b774668891921a4aa44908f" is a dyamic (map image) layer.

0 Kudos
JacksonTrappett
Occasional Contributor II

I also tried:

if(typeof(element) === MapImageLayer)

with the same result - it is false for all 4 layers.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Jackson,

   Sorry for the delay.

        arcgisUtils.createMap("4abe6a830b8f466dacf8abfde567a781", "map").then(function (response) {
          var legendLayers = arcgisUtils.getLegendLayers(response);
          legendLayers.forEach(function(element) {
            console.info(element.layer.declaredClass);
          });
        });

You can use declaredClass for that

View solution in original post

JacksonTrappett
Occasional Contributor II

Thank you Robert - I tried this code and it worked, but when I create a new map to do the test, like you did in the example, it breaks other parts of my code, which I probably could have fixed given a fair amount of work.  While working on some alternate ways to implement your suggested code, I stumbled upon this thread:

Finding service layer 'declaredClass' property (type) in AMD? 

Which shows using instanceof.  I gave that a shot and it worked for me!

webMapLayers.forEach(function(element)
{

   if(element instanceof (ArcGISDynamicMapServiceLayer)) {

Thanks so much for your help!