Select to view content in your preferred language

set visibility in Legend list

2279
1
01-28-2013 09:35 AM
BillShockley
Deactivated User
I have this for my legend function:

function addLayerList(layers) {
    var layerList = [];
    dojo.forEach(layers, function(layer){
        if(layer.layerInfos){
            dojo.forEach(layer.layerInfos, function(info,index){
                info.id  = layer.id;
                info.subId = index;
                layerList.push(info);
            })
        }else{
            layerList.push(layer);
        }
    });
    if (layerList.length > 0) {
        //create a menu of layers
        layerList.reverse();
        var menu = new dijit.Menu({
            id: 'layerMenu'
        });
        dojo.forEach(layerList, function (layer) {

            menu.addChild(new dijit.CheckedMenuItem({
                label: layer.name || layer.title,
                checked: layer.visible,
                onChange: function (e) {
                    if (layer && layer.declaredClass === 'esri.layers.LayerInfo') {
                        var l = map.getLayer(layer.id);
                        var ids = []
                        if(e){
                            ids.push(layer.subId)
                        }else{
                            ids.push(-1);
                        }
                        l.setVisibleLayers(ids);
                    } else if (layer.type === 'Feature Layer'){
                        layer.setVisibility(!layer.visible);
                    }

                }
            }));
        });


        var button = new dijit.form.DropDownButton({
            label: i18n.tools.layers.label,
            id: "layerBtn",
            iconClass: "esriLayerIcon",
            title: i18n.tools.layers.title,
            dropDown: menu
        });

        dojo.byId('webmap-toolbar-center').appendChild(button.domNode);
    }


Is there a way to specify which on the my services are visible and which are not by using there ID Numbers?

Any help is much appreciated.
Bill
0 Kudos
1 Reply
DianaBenedict
Frequent Contributor
Here is a method that I use to determine if the layer is visible at the current scale.  You need to pass in the infoLayer and the mapscale.  You can get the map scale from the map.getScale() method. The info param should come from your "dojo.forEach(layer.layerInfos, function(info,index)"... info object.

function isLayerInfoVisible(info, mapScale) {
  var isVisible = true;
  
  if (info.maxScale == 0 && info.minScale == 0) {
    isVisible = true;
  } else {  
    if (info.maxScale == 0 && mapScale > info.minScale || info.minScale == 0 && mapScale < info.maxScale) {
      isVisible = false;
    } else if (info.minScale > 0 && info.maxScale > 0) {
      if (mapScale > info.minScale && mapScale < info.maxScale) {
        isVisible = true;
      } else {
        isVisible = false;
      }
    } else {
      isVisible = true;
    }
  }  
  return isVisible;
}
0 Kudos