how to get the list of layers in a map (map service)

11995
3
09-06-2013 09:57 AM
JoseSanchez
Occasional Contributor III
Hi all

I customized this sample pointing "basemap" to one of my local map services that has several layers

http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm

The function getMapLayers  shows only one layer not all of them.

function getMapLayers() {
        alert(myMap.layerIds.length);
        for (var j = 0, jl = myMap.layerIds.length; j < jl; j++) {
            var currentLayer = myMap.getLayer(myMap.layerIds);
            alert("id: " + currentLayer.id + ", visible: " + currentLayer.visible + ", opacity: " + currentLayer.opacity);
        }
    }

Is there any other sample that list all the layers in  map service and  adds them to a drop down box

thanks
0 Kudos
3 Replies
JasonZou
Occasional Contributor III
Here is the sample code to extract the layer info from each dynamic map service. You can easily modify to add each layer name into a dropdown list.

config.layers = [];
dojo.forEach(map.layerIds, function(aLayerId) {
 var serviceLayer = map.getLayer(aLayerId);
 if ("layerInfos" in serviceLayer) {
  dojo.forEach(serviceLayer.layerInfos, function(aLayerInfo) {
   config.layers.push({
    name: aLayerInfo.name,
    mapService: serviceLayer.id,
    layerId: aLayerInfo.id
   });
  });
 }
});
0 Kudos
VinayBansal
Occasional Contributor II
You can also use esri.request to get all layers info, if you need to extract all the details of service.

var requestHandle = esri.request({
            "url": <MapService_URL> + "/layers",
            "content": {
                "f": "json"
            },
            "callbackParamName": "callback"
        });
    requestHandle.then(requestSucceeded, requestFailed);

    function requestSucceeded(response, io) {
        
       
    }
    
    function requestFailed(response) {
      
    }
0 Kudos
JoseSanchez
Occasional Contributor III
This code works:


 function buildLayerList(layer) {
        var infos = layer.layerInfos, info;
        var items = [];
        for (var i = 0, il = infos.length; i < il; i++) {
            info = infos;
            if (info.defaultVisibility) {
                visible.push(info.id);
            }
            items = "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label>";
           
        }
        dojo.byId("layer_list").innerHTML = items.join();

       // layer.setVisibleLayers(visible);
      //  map.addLayer(layer);
    }


    function getMapLayers() {
        alert(myMap.layerIds.length);
        for (var j = 0, jl = myMap.layerIds.length; j < jl; j++) {
            var currentLayer = myMap.getLayer(myMap.layerIds);
            buildLayerList(currentLayer)
       //     alert("id: " + currentLayer.id + ", visible: " + currentLayer.visible + ", opacity: " + currentLayer.opacity);
        }
    }