JS API 2.2 /2.3 ArcGIS Server 10.1 and Legend - Checkbox for sublayers

658
3
05-19-2011 07:24 AM
AndyBurns
Occasional Contributor
Hi

Just working on getting a nice legend for my service. I have followed the instructions and the legend works fine however i require checkboxes for each sublayer in the service instead of the higher layer. Does anyone know how i would be able to achieve this?

I would like to do it automatically so i dont need to manually change anything when using the code in another application for example. I just would like it to loop through, see sublayers and add the option for a checkbox to turn it on/off, unsure why this is not included as it seems so common for end users to do???

thanks
0 Kudos
3 Replies
KenDoman
Occasional Contributor II
Hi

Just working on getting a nice legend for my service. I have followed the instructions and the legend works fine however i require checkboxes for each sublayer in the service instead of the higher layer. Does anyone know how i would be able to achieve this?

I would like to do it automatically so i dont need to manually change anything when using the code in another application for example. I just would like it to loop through, see sublayers and add the option for a checkbox to turn it on/off, unsure why this is not included as it seems so common for end users to do???

thanks


Try something like this:

function getSubLayers(layer) {
    var infos = layer.layerInfos, info, i, il, subLayerArray;
    for (i=0, il = infos.length; i<il; i++) {
        info = infos;
        if (!info.subLayerIds) { // if the layer doesn't have a subLayer, ...
            subLayerArray.push(info.id); // ...function adds the layer's id to an array
        }
    }
    return subLayerArray;
}
0 Kudos
AndyBurns
Occasional Contributor
thanks for the reply, will have a crack at it next week.

I see from the code that it will retrieve the sublayers....still a bit unsure how to get the check boxes in the first TOC and not in the second one, will do some investigation.

thanks again.
0 Kudos
KenDoman
Occasional Contributor II
thanks for the reply, will have a crack at it next week.

I see from the code that it will retrieve the sublayers....still a bit unsure how to get the check boxes in the first TOC and not in the second one, will do some investigation.

thanks again.


For creating the checkboxes, I'd look at the example at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/map_dynamiclayerlist.html. It works well, except if you have sublayers. That's why I use my code to get the bottom-most sublayers. I'll put my explanation below, if you want it.

Explanation:
Say you have an .mxd with this kind of layer arrangement:
Group Layer 1
--- Layer 1
--- Layer 2
--- Layer 3

In ArcMap, you need both the Group Layer 1 and Layer 1 checked to view Layer 1. However, when you publish this map to ArcGIS Server, and the buildLayerList/updateLayerVisibility example linked above, it does something different.
Published on ArcGIS Server, your map Service will look like this:
[0] Group Layer 1
   [1] Layer 1
   [2] Layer 2
   [3] Layer 3

Now, if you've created checkboxes for all four, and they're checked for Group Layer 1 and Layer 1, this is passed to updateLayerVisibility, which calls layer.setVisibleLayers([0, 1]). This treats Group Layer 1 and Layer 1 separately, and will show not just layer 1, but layers 2, 3, and another layer 1 as well. If you turn off Group layer one in your service, it will only show Layer 1.

That's why I only create checkboxes for the bottom-most layers when I have an mxd with layer groups.
0 Kudos