Select to view content in your preferred language

Layers doesn't turn on or off

656
1
08-20-2010 08:49 AM
AlejandroMorales
New Contributor
Hi everyone,
So I'm having this problem, I have a map witch I want to take all the layers it has but when I get the layers and I try to turn them off/on it doesn't happen anything ... here is my code, please if You know the answer help me.
Here is my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Create a Map</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>

    <script type="text/javascript">
        dojo.require("esri.map");


        var  myMap, myTiledMapServiceLayer, visible = []; 

        function init() {
            myMap = new esri.Map("mapDiv");

                                 myTiledMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://servarcgisprd/arcgis/rest/services/Demo_001/MapServer");
           
            if (myTiledMapServiceLayer.loaded) {
                buildLayerList(myTiledMapServiceLayer)
            }
            else {
                dojo.connect(myTiledMapServiceLayer, "onLoad", buildLayerList); 
            }
        }

        function buildLayerList(myTiledMapServiceLayer) {
            var infos = myTiledMapServiceLayer.layerInfos, info;
            var items = [];
            for (var i = 0, i1 = infos.length; i < i1; 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();

            myTiledMapServiceLayer.setVisibleLayers(visible); 
            myMap.addLayer(myTiledMapServiceLayer);
        }

        function updateLayerVisibility() {
            var inputs = dojo.query(".list_item"), input;
            visible = [];
            for (var i = 0, i1 = inputs.length; i < i1; i++) {
                if (inputs.checked) {
                    visible.push(inputs.id); 
                }
            }
            myTiledMapServiceLayer.setVisibleLayers(visible); 
        }

        dojo.addOnLoad(init);
       

    </script>
  </head>
  <body>
    
  
    <%--<input type="text" id="punto1" value="" />--%>
    <%--<input type="text" id="punto2" value="" />--%>
    <%--<input type="button" value="Ingresar" onclick="addLineToMap(dojo.byId('punto1').value,dojo.byId('punto2').value);" />--%>
    <h4>Work flow:</h4>
 <br />
    Layer List : <span id="layer_list"></span><br />
    <br />
   
    <div id="mapDiv" class="tundra" style="width:900px; height:600px; border:1px solid #000;">
        <%--<span id="info" style="position:absolute; right:25px; bottom:5px; color:#000; z-index:50;"></span>--%> 
    </div>

  </body>
  
</html>
0 Kudos
1 Reply
DavidMaltby
New Contributor
You are missing some key components:
//Use the ImageParameters to set the visible layers in the map service during ArcGISDynamicMapServiceLayer construction.
        var imageParameters = new esri.layers.ImageParameters();
        imageParameters.layerIds = [];
        imageParameters.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW;
        //can also be: LAYER_OPTION_EXCLUDE, LAYER_OPTION_HIDE, LAYER_OPTION_INCLUDE
 

myTiledMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://servarcgisprd/arcgis/rest/services/Demo_001/MapServer", {"imageParameters":imageParameters});
  <your map>.addLayer(myTiledMapServiceLayer);
0 Kudos