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);
} })
Solved! Go to Solution.
Here is a sample i made using some public data
Edit fiddle - JSFiddle
The layerInfos class has a "subLayerIds" property.
That will give you the Id's you need.
Andrew,
When I run the code, the subLayerIds is null. Also, how can I get the name of the sub-layer ?
Brian
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;
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.
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);
}
})
Here is a sample i made using some public data
Edit fiddle - JSFiddle