2

I use the "map.getFeaturesAtPixel" method to exactly get the first feature under on a mouse-click interaction.

This works as expected even though the method does not return a single feature but all the features whose graphical representation hits the clicked point.

This behaviour will be "acceptable" but it turns out that its performance decreases as soon as we zoom out and lots of features are condensed and displayed like a giant mass of features...

I know this is not the ideal way to display features on a map but it can happen in some scenarios.

Anyway, I tried what it seems to be a faster way to retrieve what I want (first feature under my click) using a "small" Extent (based on the click coordinate) and using getFeaturesIntersectingExtent. This approach is really fast but it does not work in all scenarios since it's only aware of features geometries and not their graphical representation (my needs).

Question: is there a another way to get this first-feature-under-click in an optimal way?

Alberto
  • 75
  • 4
  • 4
    Using `var firstFeature = map.forEachFeatureAtPixel(evt.pixel, function(feature) { return feature; });` might be quicker as it stops searching at the first feature instead of unnecessarily building an array of all features. – Mike Sep 20 '19 at 13:49
  • Yes! this definetely adds some light to this issue. I also helped this function adding a layer-filter function as third parameter. Thanks – Alberto Sep 23 '19 at 09:38

1 Answers1

1

you can use this to get feature on mouse click

var feature = map.forEachFeatureAtPixel(evt.pixel,
                    function (feature) {
                        return feature;
                    });
Primit
  • 825
  • 7
  • 13