I have an OpenLayers map with a WMS background layer. When clicking the map, I need to draw the WFS feature located on the click.

Note that the vector layer is not loaded yet. I must load/request the mentioned feature on clicking.

How can I make a WFS getFeature() request with the click coordinates as a query parameter?

Is there is a better approach?

1 Answer 1


Both are good alternatives. The direct WFS request should include a Contains filter, or with point-in-polygon search the Intersects filter is as usable.

<wfs:Query typeName="topp:states">
          <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">

You can even use GET instead of POST in the request but URL-encoding the XML filter makes it not look a bit ugly


Be prepared to spend some time with testing the right order of lat and lon coordinates if you start making filters because unfortunately that depends on your target server, the coordinate reference systen, and WFS version.

Using ID from GetFeatureInfo may feel double trouble but on the other hand WFS version 2.0 defines an mandatory stored query GetFeatureById (some more information in https://docs.geoserver.org/stable/en/user/services/wfs/reference.html).

The syntax of the GetFeatureById is rather simple, here an example taken from the ESRI site https://enterprise.arcgis.com/en/server/latest/publish-services/windows/communicating-with-a-wfs-service-in-a-web-browser.htm


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.