[GIS] OpenLayers – Get Type of Layer

geoserverlayersopenlayers-2

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
    document.getElementById("drpLayers").options.add(opt); 
    //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!

Best Answer

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".