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

This is part of an interface that uses a box to select features on a WMS Layer. This then writes the name of the features to a div as a list.

var recycling = new OpenLayers.Layer.WMS( 
    "Recycling Sites",
    {layers: 'GeoserverWorkspace:recycling', 'format':'image/png', 'transparent':'true'},
    {'opacity': 1.0, 'isBaseLayer': false, 'visibility': true}

 select = new OpenLayers.Layer.Vector("Selection", {styleMap: 
            new OpenLayers.Style(["select"])

map.addLayers([osmap, recycling, select])

var recycling_wfs =  new OpenLayers.Control.GetFeature({
            protocol: OpenLayers.Protocol.WFS({
            url: wfsurl,
            version: "1.1.0",
            featureType: "recycling",
            featurePrefix: "GeoserverWorkspace",
            srsName: "EPSG:27700",
            geometryName: "wkb_geometry",
            maxFeatures: 50,
            click: true
            box: true

map.addControl(recycling_wfs);"featuresselected",this, showInfo);


function showInfo(e) {

    // assign features object to a variable

    var features = e.object.features;

    //properties are accessed by using the attibutes property
    var selectedSites = "";

        for(var fid in features){
          var site = features[fid].attributes;
          var siteName =;
          selectedSites += siteName + " ";

    document.getElementById('responseText').innerHTML = "Recycling Site: " + selectedSites;

I am confused about the event object that is returned by the featuresselected event, to the callback function showInfo(). According to the OpenLayers documentation this should be an array. So if I select 2 features I should be able to access the first name by doing the following

var name = e.object.features[0]; 

However this returns an error of e.object.features[0] undefined. I am able to get the feature name if I use a For..In and iterate through the event.object.features object. Then use the resulting fid to access the feature, like so

var name = e.object.features[fid]

Is this the correct way to access the event.object's features or have I missed somthing?

share|improve this question
up vote 4 down vote accepted

replace the following line:

var features = e.object.features;


var features = e.features;
share|improve this answer
That does the trick. Altought the openlayers docs says that features are accessed via event.object. – Peter Campbell Sep 3 '12 at 9:03

try this:"featuresselected",this, function(e) {

that will show you your event and should help you proceed from there

share|improve this answer
I have already done this by creating a temporary variable and assigning e to it, then viewing in firebug. The resulting object, has an object properties and features property. However this does not then act like an array, as in e.object.features[0] is undefined, although e.object.features["fid"] returns the feature object. – Peter Campbell Sep 3 '12 at 8:58

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.