Suite

Ajout de plusieurs couches WMS dans OpenLayers


J'ai un tableau de couches que je passe àmap.addLayers(). Quand je passe le tableau à la fonction dans$(document).ready(function (){}), il ne les ajoute pas à la carte (c'est-à-diremap.layersaucune des nouvelles couches n'est ajoutée. Mais si j'ouvre la console JavaScript et lance la fonction, cela fonctionne et mes couches ajoutées apparaissent.

Voici mon code :

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, visibilité : false } ) ; // console.log(capaObjetos[i]); } for (var key in capaObjetos) { var value = capaObjetos[key]; agregadas.push(value); } }); } agrégés = [ ]; var crasicaule, microfilo, rosetofilo, submontano, perennifolia, caducifolia, encino, pino, mezquite, tascate, mesofilo, oyamel, inducidos, halofilo, naturel, cultivados, gipsofilo, riego, temporal, mezquiteraamigetos, tular, sin 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": naturel, "15": cultivados, "16": gipsofilo, "17": riego, "18": temporel, "19": mezquital, "20": tular, "21": sinvegetacion, "22": asentamientos, "23": carretera }; $(document).ready(function () { capaBase(); TreeMenu(); // ======= AJOUTER DES COUCHES À LA CARTE =========== map.addLayers(agregadas) ; });

OpenLayers est initialisé dansCapaBase().

J'ai essayé de les ajouter individuellement dans unpourboucle mais cela ne fonctionne pas non plus.

Des idées?

METTRE À JOUR

Si je modifie ma fonction de création et déplace mon JSON localement, cela fonctionne très bien.

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, visibilité : 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]; agrégadas.push(valeur); } }

Voici le JSON que j'utilise.

[{ "0": {"id":"crasicaule", "class":"MATORRAL", "field":"CVE_UNION", "values": [{"0":"MC"}], "layers" :"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "1": {"id":"microfilo", "class":"MATORRAL", "field" :"CVE_UNION", "values": [{"0":"MDM"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "2": {"id":"rosetofilo", "class":"MATORRAL", "field":"CVE_UNION", "values": [{"0":"MDR"}], "layers":" mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "3": {"id":"submontano", "class":"MATORRAL", "field":" CVE_UNION", "values": [{"0":"MSM"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "4 " : {"id":"perennifolia", "class":"SELVAS", "field":"CVE_UNION", "values": [{"0":"SAP"}], "layers":"mx : usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "5": {"id":"caducifolia", "class":"SELVAS", "field":"CVE_UNION" , "values": [{"0":"SBC"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"} , "6": {"id":"encino", "class":"BOSQUES", "field":"CVE_UNION", "values": [{"0":"BQ"},{"1": "BQP"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "7": {"id":"pino", "class ":"BOSQUES", "field":"CVE_UNION", "values": [{"0":"BP"},{"1":"BPQ"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "8": {"id":"mezquite", "class":"BOSQUES", "field":"CVE_UNION", "values " : [{"0":"BP"},{"1":"BPQ"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms "}, "9": {"id":"tascate", "class":"BOSQUES", "field":"CVE_UNION", "values": [{"0":"BJ"}], "layers ":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "10": {"id":"mesofilo", "class":"BOSQUES", "champ ":"CVE_UNION", "values": [{"0":"BM"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"} , "11": {"id":"oyamel", "class":"BOSQUES", "field":"CVE_UNION", "values": [{"0":"BA"}], "layers": "mx:usv250s5_union", "url":"http://localhost:8082/geoserve r/wms"}, "12": {"id":"inducidos", "class":"PASTIZALES", "field":"CVE_UNION", "values": [{"0":"PI"}] , "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "13": {"id":"halofilo", "class":"PASTIZALES" , "field":"CVE_UNION", "values": [{"0":"PH"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/ wms"}, "14": {"id":"natural", "class":"PASTIZALES", "field":"CVE_UNION", "values": [{"0":"PN"}], " couches":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "15": {"id":"cultivados", "class":"PASTIZALES", " field":"CVE_UNION", "values": [{"0":"PC"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms" }, "16": {"id":"gipsofilo", "class":"PASTIZALES", "field":"CVE_UNION", "values": [{"0":"PY"}], "layers" :"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "17": {"id":"riego", "class":"AGRICULTURE", "field" :"CVE_UNION", "values": [{"0":"RA"},{"1":"RAP"},{"2":"RAS"},{"3":"RP"}, {"4":"RS"},{"5":"RSP"}], "couche s":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "18": {"id":"temporel", "class":"AGRICULTURE", " field":"CVE_UNION", "values": [{"0":"TA"},{"1":"TAP"},{"2":"TAS"},{"3":"TP" },{"4":"TS"},{"5":"TSP"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms" }, "19": {"id":"mezquital", "class":"VEGETACION", "field":"CVE_UNION", "values": [{"0":"MKX"}], "layers" :"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "20": {"id":"tular", "class":"VEGETACION", "field" :"CVE_UNION", "values": [{"0":"VT"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "21": {"id":"sinvegetacion", "class":"VEGETACION", "field":"CVE_UNION", "values": [{"0":"DV"}], "layers":" mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "22": {"id":"asentamientos", "class":"INFRASTRUCTURA", "field":" CVE_UNION", "values": [{"0":"AH"}], "layers":"mx:usv250s5_union", "url":"http://localhost:8082/geoserver/wms"}, "23 " : {"id":"carretera" , "class":"INFRASTRUCTURA", "field":"EDO_CLAVE", "values": [{"0":"29"}], "layers":"tlx:carreteraTLX", "url":"http : //localhost:8082/geoserver/wms"}}]

Pour des raisons de maintenance, je souhaite conserver le JSON dans un fichier séparé et le charger dans la fonction.

Ma question mise à jour est donc la suivante : pourquoi le chargement du JSON à partir d'un fichier externe serait-il traité différemment dans OpenLayers que le chargement du JSON localement ?


essayez d'ajouter chaque couche (ou une liste de couches, c'est comme vous préférez) en fonction du succès de votre$.getJSONet appellecrear()dans votre document prêt. Sinon,map.addLayers([])sera appelé avant que la réponse ne soit terminée… il essaiera donc d'ajouter une liste vide

function crear( n ){ $.getJSON('json/capas.json', function(data) { console.log(data); for (var i=0; i < n.length; i++) { wmsLayer = 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, visibilité : false }); map. addLayer(wmsLayer); } } } $(document).ready(function () { crear(n); }