Is there a way to get the type of all the layers in an OpenLayers maps object? What I am trying to do is iterate over all the layers in my OpenLayers map object and populate a drop-down list with the names of only the vector layers. All my vector layers come from a remote GeoServer and are WFS.

Here is the code I currently have that gets the names of the layers and populates a drop-down with the names. This code works fine;

function listLayers() {
var mLayers = map.layers; 
//set a variable to the layers array in the map object
document.getElementById("drpLayers").options.length = 0; 
// clear any existing options from the drop-down list

for (var a = 0; a < mLayers.length; a++) { 
    //loop through the map layers array
    var opt = document.createElement("option");
    //create an option object in the drop-down
    opt.text = mLayers[a].name; 
    //add the name of the layer for this drop-down list option to the text attribute of the option object
    opt.value = mLayers[a].name; 
    //add the name of the layer for this drop-down list option to the value attribute of the option object
    //add the option object to the drop-down list

I thought I could just modify this code to something like this:

opt.text = mLayers[a].type 

or perhaps this:

opt.text = mLayers[a].class

but neither way works. When I look at the OpenLayers documentation I cannot find a layer property that describes the layer type. Any suggestions would be appreciated!

2 Answers 2


Try with a code like this

var layers = map.layers[0];

You should get a string like "OpenLayers.Layer.OSM" or "OpenLayers.Layer.WMS".

  • Dariapra - You have solved the problem. Thank you for sharing you knowledge. Here is the code I used to loop through the layers in my map: function getLayerByClass() { var mLayers = map.layers; for (var a = 0; a < mLayers.length; a++) { var str = "Layer name is: " + mLayers[a].name + " Layer geo_type is: " + mLayers[a].CLASS_NAME; alert(str); }; Nov 18, 2012 at 20:50

As in this answer - you can grab a list of your Vector layers directly, simply by stating your target class type:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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