<div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">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 style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">This function creates the layers:</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">function crear( n ){</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> $.getJSON('json/capas.json', function(data) {</div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> console.log(data);</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> for (var i=0; i < n.length; i++) {</div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> capaObjetos[i] = new OpenLayers.Layer.WMS(</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> data[0][i]["id"],</div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> data[0][i]["url"],</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> {</div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> "layers": data[0][i]["layers"],</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
transparent: true,</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> format:"image/png",</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
CQL_FILTER: queryCQL(i),</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> projection: map.projection,</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
displayOutsideMaxExtent: false</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> },</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
{</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> singleTile: true,</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
ratio: 1,</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> buffer: 0,</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
visibility: false</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> }</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
);</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> // console.log(capaObjetos[i]);</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> </div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> }</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> for (var key in capaObjetos) {</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
var value = capaObjetos[key];</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> agregadas.push(value);</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
}</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> });</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"> }</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
agregadas = [ ];</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><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 style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><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 style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">});</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">Thanks</div><div class="" style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<div id=":k8" class="" tabindex="0"><img class="" src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif"></div></div></div><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 class="h5">
<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>