moralexluis

Layers doesn't turn on or off

Discussion created by moralexluis on Aug 20, 2010
Latest reply on Aug 26, 2010 by drmaltby
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[i];
                if (info.defaultVisibility) {
                    visible.push(info.id);
                }
                items[i] = "<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[i].checked) {
                    visible.push(inputs[i].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>

Outcomes