Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to get the type of all the layers in an OpenLayer maps object? What I am trying to do is iterate over all the layers in my OpenLayers map object and populate a dropdown 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 dropdown 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 dropdown 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 dropdown
    opt.text = mLayers[a].name; //add the name of the layer for this dropdown list option to the text attribute of the option object
    opt.value = mLayers[a].name; //add the name of the layer for this dropdown list option to the value attribute of the option object
    document.getElementById("drpLayers").options.add(opt); //add the option object to the dropdown 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!

share|improve this question
up vote 2 down vote accepted

Try with a code like this

var layers = map.layers[0];

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

share|improve this answer
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); }; – Todd Krueger Nov 18 '12 at 20:50

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


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.