[GIS] Having trouble with the getFeaturesAtCoordinat() method in OpenLayers

jqueryopenlayersopenlayers-2

Why reason when I do click in a polygon, it works and not in a point?

I have the following jQuery Code:

var despachosFeatures = despachosWFS.getFeaturesAtCoordinate(evt.coordinate);
var despachosJSONByWFS = format.writeFeatures(despachosFeatures);

var grifosFeatures = grifosWFS.getFeaturesAtCoordinate(evt.coordinate);
var grifosJSONByWFS = format.writeFeatures(grifosFeatures);

In the first and second line code, It returns a JSON with all features on the source (It's a polygon).

But, third and fourth line code, it doesn't return nothing. Why is the reason?

{"type":"FeatureCollection","features":[]}

I've checked the vector source "grifosWFS" and it works perfectly. Really I don't understand anything.

Any ideas?

Best Answer

According to the code, getFeaturesAtCoordinate needs a method containsXY on the geometry class to works. But this function is not defined for point geometry and the parent class return always falsesee here.

May be the use of ol.source.Vector.getClosestFeatureToCoordinate is more appropriate for a list of point features.

Or you can provide a buffer around the coordinate of your point and use ol.source.Vector.forEachFeatureInExtent (unstable)

See the api of vector source class

Hope this helps