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?