using layer name instead of layer id

7267
8
01-11-2011 08:06 AM
SteveSkelton1
New Contributor
I'm a real newbie and have a web app that embeds an ArcGIS map using the JavaScript API. I used the sample code from the following and have my app working:

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples/map_explicitlayerlist.html

If you visit the URL above, you'll see it uses layer ids for the default layers to be displayed (i.e., "imageParameters.layerIds = [2];" and "visible = [2];"). I currently have mine set to use layer ids, but I needed more than 1 layer displayed by default, so I currently have: "imageParameters.layerIds = [0,1,20,21,22];" and "visible = [0,1,20,21,22];".

However, over time, the layer order will change because I'll be adding new layers. Therefore, I want to refer to the layers by name, instead of id.

How do I modify the code at the sample URL above to set the default layers to display using layer names, instead of layer ids?

Thanks,
Steve
0 Kudos
8 Replies
HemingZhu
Occasional Contributor III
I'm a real newbie and have a web app that embeds an ArcGIS map using the JavaScript API. I used the sample code from the following and have my app working:

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples/map_explicitlayerlist.html

If you visit the URL above, you'll see it uses layer ids for the default layers to be displayed (i.e., "imageParameters.layerIds = [2];" and "visible = [2];"). I currently have mine set to use layer ids, but I needed more than 1 layer displayed by default, so I currently have: "imageParameters.layerIds = [0,1,20,21,22];" and "visible = [0,1,20,21,22];".

However, over time, the layer order will change because I'll be adding new layers. Therefore, I want to refer to the layers by name, instead of id.

How do I modify the code at the sample URL above to set the default layers to display using layer names, instead of layer ids?

Thanks,
Steve


For both ArcGISDynamicMapServiceLayer and ArcGISTiledMapServiceLayer, there is a property call layerInfos which is LayerInfo[]. Each element of the array is a object of LayerInfo which has id, name etc properties. You can use it for your purpose. See Javascript API Reference for more detail.
0 Kudos
SteveSkelton1
New Contributor
For both ArcGISDynamicMapServiceLayer and ArcGISTiledMapServiceLayer, there is a property call layerInfos which is LayerInfo[]. Each element of the array is a object of LayerInfo which has id, name etc properties. You can use it for your purpose. See Javascript API Reference for more detail.


As I said, I'm a newbie. If anyone wants to give me some sample code to modify the code at the URL I listed above to use layer names via LayerInfo, I'd appreciate it. Otherwise, I'm off to see if I can figure it out myself.

Thanks for all the help,
Steve
0 Kudos
SteveSkelton1
New Contributor
0 Kudos
AdrianMarsden
Occasional Contributor III
Steve - two days before Christmas and I am running down for the year - any chance you could post what you did?  I was after exactly the same.

I've got to e-write my ArcIMS code next year, and replacing IDs with names was a crucial step in my old IMS stuff - also generously written by someone else!

Many thanks
ACM
0 Kudos
SteveSkelton1
New Contributor
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples_start.htm
Map | Create Layer List  (http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples/map_dynamiclayerlist.html)

The sample code above shows you how to loop through and display all layers with checkboxes. This sample should work for you if you want all layers included.

I didn't want some of the layers shown in the layer list. Below is code for my current buildLayerList function. I added an IF statement to capture the layers I wanted included. However, FYI, comparing what I have now to what they have in sample code at link above, it looks like something's a little different from when I first got the sample code from them.

      function buildLayerList(layer) {
        var infos = layer.layerInfos, info;
        var items = [];
        for (var i=0, il=infos.length; i<il; i++) {
          info = infos;
          if (info.name == 'Sample Layer Name #1' || info.name == 'Sample Layer Name #2' || info.name == 'Sample Layer Name #3') {
            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);

      }
0 Kudos
AdrianMarsden
Occasional Contributor III
many thanks - I'll take a good look at that next year, it seems to be a good starting point

Cheers

ACM
0 Kudos
ChrisSergent
Regular Contributor III

Do you have an updated version of this code?

0 Kudos
irtizahussain
Occasional Contributor

I want the updated version of this code !! Anyone help kindly

0 Kudos