I am using OpenLayers to send a GetFeature request to GeoServer. With the help of Firebug I can see that the response contains some of the attribute data I need. I am interested in showing some attributes once the user clicked on a feature.
How can I make sure that GeoServer sends all attributes and how can I read them in OpenLayers?
Here is my code, for now I only succeeded at displaying the feature selected on my map using an OpenLayers example:
Feature = new OpenLayers.Layer.WMS(
"Ramsar sites",
"http://ramsardev.grid.unep.ch:8080/geoserver/wms",
{
layers:'ramsar_sdi:features',
transparent:'true',
visibility: true,
isBaseLayer: false
});
map.addLayer(Feature);
select = new OpenLayers.Layer.Vector("Selection", {styleMap: new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])});
map.addLayer(select);
control = new OpenLayers.Control.GetFeature({
protocol: OpenLayers.Protocol.WFS.fromWMSLayer(Feature),
box: false,
hover: false,
multipleKey: "shiftKey",
toggleKey: "ctrlKey"
});
control.events.register("featureselected", this, function(e) {
select.addFeatures([e.feature]);
});
control.events.register("featureunselected", this, function(e) {
select.removeFeatures([e.feature]);
});
map.addControl(control);
control.activate();
The problem is that I can't see any of my attributes anywhere in the event object e
when the event featureselected
is triggered.
Best Answer
Actually you should be able to get the attribute value like this:
But if it does not work you could try one of these options:
If I see correctly you have a wms layer which you convert to a wfs layer and you try to get the attributes... if this is correct, I think you should consider using GetFeatureInfo, which shows the whole attribute table of the clicked feature, based on the wms layer here is an example:
where the attributes show up in the html div "attr"
Or if there is a reason you want to use the getfeature request (e.g dont want to get all of the attributes) then use a WFS request instead of wms (could be slow), like this:
and then use a selectcontrol to get the attributes of the feature this way you can write it to a div or something