<div dir="ltr">Upon further review, it works only if my JSON is local and not separated from my code.<div><br></div><div>This works:</div><div><div><br></div><div>      function crear( n ){</div><div>          console.log("crear capa: " + n);</div>

<div><br></div><div>          for (var i=0; i < n.length; i++) {   </div><div>              capaObjetos[i] = new OpenLayers.Layer.WMS(</div><div>                              capas[0][i]["id"],</div><div>                              capas[0][i]["url"],</div>

<div>                              {</div><div>                              "layers": capas[0][i]["layers"],</div><div>                                transparent: true,</div><div>                                format:"image/png",</div>

<div>                                CQL_FILTER: queryCQL(i),</div><div>                                projection: map.projection,</div><div>                                displayOutsideMaxExtent: false</div><div>                              },</div>

<div>                              {</div><div>                                singleTile: true,</div><div>                                ratio: 1,</div><div>                                buffer: 0,</div><div>                                visibility: false</div>

<div>                              }</div><div>                          );</div><div>              console.log(capaObjetos[i]);</div><div>        }</div><div>        // push capas de 'USO DEL SUELO Y VEGETACION' a una arregla, agregadas</div>

<div>                  for(var key in capaObjetos) {</div><div>                      var value = capaObjetos[key];</div><div>                      agregadas.push(value);</div><div>                  }</div><div>      }</div>

</div><div><br></div><div><br></div><div>While this doesn't:</div><div><br></div><div><div>function crear( n ){</div><div>          console.log("crear function");</div><div>          alert(n);</div><div>          // agregadas = [];</div>

<div>          console.log("getting JSON..");</div><div>            $.getJSON('json/capas.json', function(data) {</div><div>                console.log(data);</div><div>              for (var i=0; i < n.length; i++) {</div>

<div>                        capaObjetos[i] = new OpenLayers.Layer.WMS(</div><div>                                      data[0][i]["id"],</div><div>                                      data[0][i]["url"],</div>

<div>                                      {</div><div>                                      "layers": data[0][i]["layers"],</div><div>                                        transparent: true,</div><div>

                                        format:"image/png",</div><div>                                        CQL_FILTER: queryCQL(i),</div><div>                                        projection: map.projection,</div>

<div>                                        displayOutsideMaxExtent: false</div><div>                                      },</div><div>                                      {</div><div>                                        singleTile: true,</div>

<div>                                        ratio: 1,</div><div>                                        buffer: 0,</div><div>                                        visibility: false</div><div>                                      }</div>

<div>                                  );</div><div>                      // console.log(capaObjetos[i]);</div><div><br></div><div>                      </div><div>              }</div><div>                 </div><div>                for (var key in capaObjetos) {</div>

<div>                      var value = capaObjetos[key];</div><div>                      agregadas.push(value);</div><div>                }</div><div><br></div><div>            });</div><div>    }</div></div><div><br></div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 18, 2014 at 11:42 AM, Zach Seaman <span dir="ltr"><<a href="mailto:znseaman@gmail.com" target="_blank">znseaman@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>OL is initialized. I'm adding them via .addLayer([agregadas]). It's very odd to me why it would work in the console but not on the page load.</div>

<div><br></div><div>This function creates the layers:</div>
<div> </div><div>function crear( n ){</div><div>            $.getJSON('json/capas.json', function(data) {</div><div>                console.log(data);</div><div>              for (var i=0; i < n.length; i++) {</div>


<div>                        capaObjetos[i] = new OpenLayers.Layer.WMS(</div><div>                                      data[0][i]["id"],</div><div>                                      data[0][i]["url"],</div>


<div>                                      {</div><div>                                      "layers": data[0][i]["layers"],</div><div>                                        transparent: true,</div><div>


                                        format:"image/png",</div><div>                                        CQL_FILTER: queryCQL(i),</div><div>                                        projection: map.projection,</div>


<div>                                        displayOutsideMaxExtent: false</div><div>                                      },</div><div>                                      {</div><div>                                        singleTile: true,</div>


<div>                                        ratio: 1,</div><div>                                        buffer: 0,</div><div>                                        visibility: false</div><div>                                      }</div>


<div>                                  );</div><div>                      // console.log(capaObjetos[i]);</div><div><br></div><div>                      </div><div>              }</div><div>                 </div><div>                for (var key in capaObjetos) {</div>


<div>                      var value = capaObjetos[key];</div><div>                      agregadas.push(value);</div><div>                }</div><div><br></div><div>            });</div><div><br></div><div>    }</div><div>


<br></div><div>agregadas = [ ];</div><div><br></div><div><div>// Variables de Capas para capaObjectos</div><div>    var crasicaule, microfilo, rosetofilo, submontano, perennifolia, caducifolia, encino, pino, mezquite, tascate, mesofilo, oyamel, inducidos, halofilo, </div>


<div>    natural, cultivados, gipsofilo, riego, temporal, mezquital, tular, sinvegetacion, asentamientos, carretera;</div><div><br></div><div>    var capaObjetos = {</div><div>              "0": crasicaule,</div>


<div>              "1": microfilo,</div><div>              "2": rosetofilo,</div><div>              "3": submontano,</div><div>              "4": perennifolia,</div><div>              "5": caducifolia,</div>


<div>              "6": encino,</div><div>              "7": pino,</div><div>              "8": mezquite,</div><div>              "9": tascate,</div><div>              "10": mesofilo,</div>


<div>              "11": oyamel,</div><div>              "12": inducidos, </div><div>              "13": halofilo,</div><div>              "14": natural,</div><div>              "15": cultivados,</div>


<div>              "16": gipsofilo,</div><div>              "17": riego, </div><div>              "18": temporal,</div><div>              "19": mezquital,</div><div>              "20": tular,</div>


<div>              "21": sinvegetacion,</div><div>              "22": asentamientos,</div><div>              "23": carretera</div><div>    };</div></div><div><br></div><div><div>$(document).ready(function () {</div>


<div>                capaBase();</div><div>                TreeMenu();</div><div>                </div><div>    // ======= AGREGAR CAPAS DE 'USO DEL SUELO' AL MAPA ===========</div><div>                </div><div>


               map.addLayers(agregadas);</div></div><div><br></div><div>});</div><div><br></div><div><br></div><div>Thanks</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">

On Mon, Aug 18, 2014 at 11:30 AM, TDS <span dir="ltr"><<a href="mailto:tds@tds-net.de" target="_blank">tds@tds-net.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div>Hello Zach,<br>
      <br>
      please give us an example of your code. Is OL already initialized?
      Do you add the layers with brackets (array) =>
      .addLayers([layer1, layer2]) ? Only .addLayer() is without
      brackets.<br>
      <br>
      <div>Bye, TDS
        <hr>
        <font size="-1">
          <a href="mailto:tds@tds-net.de" target="_blank">mailto:tds@tds-net.de</a><br>
        </font>
        <hr>
        <font size="-1">
          1+1=10<br>
          You have a question? - 42 or RTFM.
        </font>
      </div>
      Am 18.08.2014 um 18:07 schrieb Zach Seaman:<br>
    </div>
    <blockquote type="cite"><div><div>
      <div dir="ltr">
        <p>
          I have an array of layers that I am passing to <code>map.addLayers()</code>.
          When I pass the array to the function in <code>$(document).ready(function
            (){})</code>, it doesn't add them to the map (i.e. <code>map.layers</code> does
          not have any of the new layers added. But if I open up the
          JavaScript console and run the function, it works and my added
          layers appear.</p>
        <p>
          I've tried adding them individually in a <code>for</code> loop
          but that also doesn't work either.</p>
        <p>
          Any ideas?</p>
        <p>
          Zach</p>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      </div></div><pre>_______________________________________________
Users mailing list
<a href="mailto:Users@lists.osgeo.org" target="_blank">Users@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-users" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-users</a></pre>
    </blockquote>
    <br>
  </div>

</blockquote></div><br></div>
</div></div></blockquote></div><br></div>