[OpenLayers-Users] Adding multiple WMS layers in OpenLayers

Zach Seaman znseaman at gmail.com
Mon Aug 18 12:02:04 PDT 2014


Upon further review, it works only if my JSON is local and not separated
from my code.

This works:

      function crear( n ){
          console.log("crear capa: " + n);

          for (var i=0; i < n.length; i++) {
              capaObjetos[i] = new OpenLayers.Layer.WMS(
                              capas[0][i]["id"],
                              capas[0][i]["url"],
                              {
                              "layers": capas[0][i]["layers"],
                                transparent: true,
                                format:"image/png",
                                CQL_FILTER: queryCQL(i),
                                projection: map.projection,
                                displayOutsideMaxExtent: false
                              },
                              {
                                singleTile: true,
                                ratio: 1,
                                buffer: 0,
                                visibility: false
                              }
                          );
              console.log(capaObjetos[i]);
        }
        // push capas de 'USO DEL SUELO Y VEGETACION' a una arregla,
agregadas
                  for(var key in capaObjetos) {
                      var value = capaObjetos[key];
                      agregadas.push(value);
                  }
      }


While this doesn't:

function crear( n ){
          console.log("crear function");
          alert(n);
          // agregadas = [];
          console.log("getting JSON..");
            $.getJSON('json/capas.json', function(data) {
                console.log(data);
              for (var i=0; i < n.length; i++) {
                        capaObjetos[i] = new OpenLayers.Layer.WMS(
                                      data[0][i]["id"],
                                      data[0][i]["url"],
                                      {
                                      "layers": data[0][i]["layers"],
                                        transparent: true,
                                        format:"image/png",
                                        CQL_FILTER: queryCQL(i),
                                        projection: map.projection,
                                        displayOutsideMaxExtent: false
                                      },
                                      {
                                        singleTile: true,
                                        ratio: 1,
                                        buffer: 0,
                                        visibility: false
                                      }
                                  );
                      // console.log(capaObjetos[i]);


              }

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

            });
    }



On Mon, Aug 18, 2014 at 11:42 AM, Zach Seaman <znseaman at gmail.com> wrote:

> 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.
>
> This function creates the layers:
>
> function crear( n ){
>             $.getJSON('json/capas.json', function(data) {
>                 console.log(data);
>               for (var i=0; i < n.length; i++) {
>                         capaObjetos[i] = new OpenLayers.Layer.WMS(
>                                       data[0][i]["id"],
>                                       data[0][i]["url"],
>                                       {
>                                       "layers": data[0][i]["layers"],
>                                         transparent: true,
>                                         format:"image/png",
>                                         CQL_FILTER: queryCQL(i),
>                                         projection: map.projection,
>                                         displayOutsideMaxExtent: false
>                                       },
>                                       {
>                                         singleTile: true,
>                                         ratio: 1,
>                                         buffer: 0,
>                                         visibility: false
>                                       }
>                                   );
>                       // console.log(capaObjetos[i]);
>
>
>               }
>
>                 for (var key in capaObjetos) {
>                       var value = capaObjetos[key];
>                       agregadas.push(value);
>                 }
>
>             });
>
>     }
>
> agregadas = [ ];
>
> // Variables de Capas para capaObjectos
>     var crasicaule, microfilo, rosetofilo, submontano, perennifolia,
> caducifolia, encino, pino, mezquite, tascate, mesofilo, oyamel, inducidos,
> halofilo,
>     natural, cultivados, gipsofilo, riego, temporal, mezquital, tular,
> sinvegetacion, asentamientos, carretera;
>
>     var capaObjetos = {
>               "0": crasicaule,
>               "1": microfilo,
>               "2": rosetofilo,
>               "3": submontano,
>               "4": perennifolia,
>               "5": caducifolia,
>               "6": encino,
>               "7": pino,
>               "8": mezquite,
>               "9": tascate,
>               "10": mesofilo,
>               "11": oyamel,
>               "12": inducidos,
>               "13": halofilo,
>               "14": natural,
>               "15": cultivados,
>               "16": gipsofilo,
>               "17": riego,
>               "18": temporal,
>               "19": mezquital,
>               "20": tular,
>               "21": sinvegetacion,
>               "22": asentamientos,
>               "23": carretera
>     };
>
> $(document).ready(function () {
>                 capaBase();
>                 TreeMenu();
>
>     // ======= AGREGAR CAPAS DE 'USO DEL SUELO' AL MAPA ===========
>
>                map.addLayers(agregadas);
>
> });
>
>
> Thanks
>
>
>
> On Mon, Aug 18, 2014 at 11:30 AM, TDS <tds at tds-net.de> wrote:
>
>>  Hello Zach,
>>
>> 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.
>>
>> Bye, TDS
>> ------------------------------
>>  mailto:tds at tds-net.de <tds at tds-net.de>
>>  ------------------------------
>>  1+1=10
>> You have a question? - 42 or RTFM.
>> Am 18.08.2014 um 18:07 schrieb Zach Seaman:
>>
>>  I have an array of layers that I am passing to map.addLayers(). When I
>> pass the array to the function in $(document).ready(function (){}), it
>> doesn't add them to the map (i.e. map.layers 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.
>>
>> I've tried adding them individually in a for loop but that also doesn't
>> work either.
>>
>> Any ideas?
>>
>> Zach
>>
>>
>> _______________________________________________
>> Users mailing listUsers at lists.osgeo.orghttp://lists.osgeo.org/mailman/listinfo/openlayers-users
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20140818/3b2356ef/attachment.html>


More information about the Users mailing list