Is it possible to Toggle map service layers by name (not id) ?

559
1
07-16-2012 09:01 AM
ChristopherPollard
Occasional Contributor
I need to toggle Dynamic Map service layers by Layer Name instead of Layer id.
Is it possible for me to do this using layerinfo.name?
I have modifed the Explicity Create Map Service Layer List sample http://help.arcgis.com/en/webapi/javascript/arcgis/demos/map/map_explicitlayerlist.html for what I need
and I have been having issues trying to get layerinfo.name defined.

I have followed a few example in other posts and can't quite get the samples to work in my favor.
I saw the recent post regarding the 'Legend with Visible Layers' and couldn't figure out how to breakdown that sample to manually build the layer list.
I have attacthed my working sample using Layer id.

One thought I had was to write a function that would take the layer name from the check box function, Get/return the layer id, and use that result to toggel the layers.
I am still developing my javascript skills but would be open to any suggestions.

My ArcGIS server is 9.31 so I can't take advantage of the Legend widget or other samples.
Thanks,
Chris
0 Kudos
1 Reply
SiqiLi
by Esri Contributor
Esri Contributor
The ArcGISDynamicMapServiceLayer.setVisibleLayers(ids, doNotRefresh?) method only takes an array of layer IDs. But, you can use a 2D array to hold the layer name and id information, which can be used later to get the layer's id from this 2D array based on the layer's name.

For example:
var items = [["One", 0],["Two", 1],["Three", 2]];  //format: [name,id] Note: check service REST endpoint to get the information about layer name and id 
var visible =[];
var lyrName = "Three"; //layer wants to be turned on
for (var i = 0; i < items.length; i++){
    if (items[0] == lyrName) {
     visible.push(items[1]);
     break;
    } 
} 
agsDynamicMapServiceLayer.setVisibleLayers(visible);    
0 Kudos