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.

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

2 Answers 2

up vote 2 down vote accepted

Try with a code like this

...
var layers = map.layers[0];
alert(layers[0].CLASS_NAME);
...

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:

map.getLayersByClass("OpenLayers.Layer.Vector")

share|improve this answer

Your Answer

 
discard

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.