Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using OpenLayers, Geoserver 2.1 CQL language. The problem is that I do not know how to address the layer. I am trying to call a function like this:

   var createWmsLayer = function(name, url, params, options) {
        map.addLayer(new OpenLayers.Layer.WMS(name, url, params, options));         

I am creating the layer like this:

var ikke_listet = new  createWmsLayer("<span class='lc'>Ikke rødlistet</span>", 
                {layers: 'gbif_ns:ikke_listet',format: 'image/png', 
                transparent: 'true'}, {singleTile: true, ratio: 1, 
                  isBaseLayer:false, visibility: false}); 

Then I am calling a function on the layer to redraw it with CQL params to GeoServer.

function CQLfilter(param){
        filter_list = [] 
        filter_list.append("collector LIKE '%H%' ")
        ikke_listet.mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})

But I get this error in Firebug:

ikke_listet is not defined

So my question is: What is the name of my layer?

I am using the "name" part of my function to write out some styles for the layerlist (I use ExtJS) and put a padding and different icons in the layerlist to show what icons represent which layer. Is this a wrong way to do this styling?

Any suggestions as to what I use in the function to add CQL call to the layer and redraw it?

Aragon, I was able to get the layername from your suggestion of the layer array:

function CQLfilter(param){
var mLayers = map.layers;
mLayers[9].mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})
// This gave me the names of the layers
// for(var a = 0; a < mLayers.length; a++ ){
//    alert(mLayers[a].name + '#' + a + '#')

So I wonder if there is another way to style my layer list because now I get the layernames like this: "<span class='lc'>Ikke rødlistet</span>". But of course I can still get to the layer by referencing the layer array position.

I managed to run the function now and it filters correctly. I only had to ignore the first base 4 layers and I have to find out how to collect the values from the search form. But manually this code now works and I have much progress :-)

function CQLfilter(param){
var mLayers = map.layers;
// have to get the search by document.getElementById or jQuery I guess. Testing manually now
var search = "kingdom LIKE '%Plantae%' ";
for(var a = 4; a < (mLayers.length); a++ ){
    alert(mLayers[a].name + '#' + a + '#')
    mLayers[a].mergeNewParams({'CQL_FILTER': search})

Hi again, I am able to get the layer now. Works fine with this function on the CQL filter and the reset filter function at the end.

function CQLfilter(sok,cat){
var mLayers = map.layers;
switch (cat)
case 'SN':
   param = "scientificname LIKE '%"+ sok + "%' ";
case 'NNG':
   param = "norsknavngruppe LIKE '%"+ sok + "%' ";
case 'NNA':
   param = "norsknavnart LIKE '%"+ sok + "%' ";
case 'PROJ':
   param2 = new Array()
   param2.push("prosjektnummer LIKE '%" + sok + "%'")
   param2.push("prosjektnavn LIKE '%" + sok + "%'")
   param = param2.join(" OR ");
   param2 = new Array()
   param2.push("scientificname LIKE '%" + sok + "%'")
   param2.push("norsknavngruppe LIKE '%" + sok + "%'")
   param2.push("norsknavnart LIKE '%" + sok + "%'")
   param2.push("collector LIKE '%" + sok + "%'")
   param = param2.join(" OR ");
for(var a = 4; a < (mLayers.length); a++ ){
    mLayers[a].mergeNewParams({'CQL_FILTER': param});
function resetCQLfilters() {
    var mLayers = map.layers;
    for(var a = 4; a < (mLayers.length); a++ ){
    delete mLayers[a].params.CQL_FILTER;
share|improve this question

3 Answers 3

up vote 9 down vote accepted

which layer do you want, i dont understand? but getting all layer:


getting all layer name:

var mLayers = map.layers;

for(var a = 0; a < mLayers.length; a++ ){

getting last added layer name:


and beside this you can use all this method too:


i hope it helps you...

share|improve this answer
I am trying to access the layer "gbif_ns:ikke_listet" but when I alert the layername it says the layername is "<span class='lc'>Ikke rødlistet</span>" - that is of course since I use the layername to style it. You can see it here: babkart.no I am really stuck here on some basic js obiously. –  asle May 14 '12 at 13:34
you can add new defination to your layer properties sth. like this: map.layers[1].orgName = "gbif_ns:ikke_listet" than you can easily reach it... –  Aragon May 14 '12 at 13:47
Would that be inside the init function (on body onload) just after the layer declarations? I will try this. Thanks! –  asle May 14 '12 at 15:59
you will add this when creating a new layer. it will be properties of your newly created layer. –  Aragon May 14 '12 at 17:10
Thanks Aragon, look at my comments in the original question. I hope using map.layers array is an ok way to handle it! –  asle May 18 '12 at 12:22

I think your scoping has gone haywire.

(if i understand your code corrctly) You have two functions: createWmsLayer and CQLfilter.

in the line var ikke_listet = new createWmsLayer...

you assign the returned value of the call to createWmsLayer to the variable ikke_listet. The problem is that createWmsLayer does not return anything.

So, you should rewrite it to :

var createWmsLayer = function(name, url, params, options) {
    var layer = new OpenLayers.Layer.WMS(name, url, params, options);
    return layer;

as variables created inside functions are not available outside (scoping).

On another note: while you do reach map inside the createWmsLayer fucntion, this is only because you have defined the map variable before the you define the function, so you should pass that as an input parameter as well.

But, basically; remember to return any variables created inside a function in order to use them outside of the function

EDIT: and skip the "new" keyword when calling your createWmsLayer function

share|improve this answer
Hi, I changed the createWmsLayer code and removed the "new" keyword. Try to create it this way: var ikke_listet = createWmsLayer( "<span class='lc'>Ikke listet</span>", "localhost/geoserver/wms";, {layers: 'gbif_ns:ikke_rodlistet',format: 'image/png', transparent: 'true'}, {singleTile: true, ratio: 1, isBaseLayer:false, visibility: false} ); I still get this error: ikke_listet is not defined ikke_listet.mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "}) –  asle May 15 '12 at 8:13
I tried to define the function CQLfilter() both inside the app and outside with no help. I must be doing something totaly newbie wrong here :-( –  asle May 15 '12 at 8:13
if you could post a more detailed version of your code on pastebin or something i could take a look. But, it seems as though your main problem is Javascript itself, not OpenLayers as such –  atlefren May 15 '12 at 11:23
Here is my js - app.js pastebin.com/zHYQP5qZ Here is my index file: index.html pastebin.com/TW4Aqz4Q Here is the site at the moment: babkart.no –  asle May 15 '12 at 15:51

you can try by using Firebug (with firefox browser) and insert "console.dir(map.layers);" in your javascript code to show layer details.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.