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.

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",
    wmsurl+'?', 
    {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(OpenLayers.Feature.Vector.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);  

recycling_wfs.events.register("featuresselected",this, showInfo);

recycling_wfs.activate();

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 = site.name;
          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].attributes.name; 

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].attributes.name

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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

replace the following line:

var features = e.object.features;

for:

var features = e.features;
share|improve this answer
1  
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:

recycling_wfs.events.register("featuresselected",this, function(e) {
console.log(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

 
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.