Select to view content in your preferred language

iterate through sub-layers

2373
6
Jump to solution
09-13-2016 08:34 AM
BrianFoley1
Emerging Contributor

I have a dynamicmapservicelayer (dynaLayer1) that has three layers. Each layer then has at least 2 sub-layers beneath it. 

dynaLayer1

----> Layer 1

--------> Sub-Layer 1

--------> Sub-Layer 2

----> Layer 2

--------> Sub-Layer 1

--------> Sub-Layer 2

----> Layer 3

--------> Sub-Layer 1

--------> Sub-Layer 2

I am using the following code to get the layers.  How can I get to the sub-layers (Sub-Layer 1 & 2) below that ?

Thanks.

dojo.connect(dynaLayer1, "onLoad", function () {
    var infos = dynaLayer1.layerInfos;
        for (var i = 0; i <= infos.length - 1; i++) {  
                var layerId = infos.id;
                var layerName = infos.name;
                var layerVisible = infos.defaultVisibility;
                       

                alert("layerId : " + layerId + ".....layerName : " + layerName + ".....visible : " + layerVisible);
         }    })

0 Kudos
1 Solution

Accepted Solutions
Drew
by
Frequent Contributor

Here is a sample i made using some public data
Edit fiddle - JSFiddle 

View solution in original post

6 Replies
Drew
by
Frequent Contributor

The layerInfos class has a "subLayerIds" property.
That will give you the Id's you need.

LayerInfo | API Reference | ArcGIS API for JavaScript 3.17 

BrianFoley1
Emerging Contributor

Andrew,

When I run the code, the subLayerIds is null. Also, how can I get the name of the sub-layer ?

Brian

0 Kudos
Drew
by
Frequent Contributor

If "subLayerIds" is null then something else is wrong. Try publishing your service again and look at the REST endpoint to ensure they exist.

To get the name you just need to get the LayerInfo of the item..

var layerName = infos[mySubLayerId].name;
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

The subLayerIds will always be null if it is not a parent layer(meaning that the layer is not a grouped layer). You should check if the parentLayerId is not null before worrying about subLayerIds.

BrianFoley1
Emerging Contributor

The parentId is -1. 

This is my code :

var initialExtent = new Extent
(
        {
                 xmin: -8119646.38042271,
                 ymin: 4986891.42830988,
                 xmax: -8102329.13378822,
                 ymax: 4999699.14992532,
                  "spatialReference": { "wkid": 102100 }
          }
 );

 map = new Map("map", {
            extent: initialExtent,
            smartNavigation: false,
            slider: false,
            zoom: 14
 });

baseLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(baseLayer);

dynaLayer1 = new esri.layers.ArcGISDynamicMapServiceLayer ( "https://xxxxxxxxxx/ArcGIS/rest/services/xxxxx/Contaminated_Areas",

       {

            id: "Contaminated Areas", opacity: 0.8

        }

);
dynaLayer1.setVisibleLayers([0, 1, 2]);

map.addLayers([dynaLayer1]);


dojo.connect(dynaLayer1, "onLoad", function () {
        var infos = dynaLayer1.layerInfos;
        for (var i = 0; i <= infos.length - 1; i++) {  
              var layerId = infos.id;
              var layerName = infos.name;
              var layerVisible = infos.defaultVisibility;

                                          

              //alert("layerId : " + layerId + ".....layerName : " + layerName + ".....visible : " + layerVisible);
        
})

0 Kudos
Drew
by
Frequent Contributor

Here is a sample i made using some public data
Edit fiddle - JSFiddle