clickopenlayersopenlayers-10

How to detect openlayers features at pixel with no fill


I just upgraded to OpenLayers 10.4 from version 4.5. I need to get all features at point, where a user clicks. So I use

map.forEachFeatureAtPixel(pixel, function (feature, layer) { .... }));

It works fine, when a fill color for a polygon at that place is specified.

new ol.style.Style({
    fill: new ol.style.Fill({ color: [0xfb, 0x7f, 0x7f, 0.7] }),
    stroke: new ol.style.Stroke({ color: "#ff0000", width: 1 })
})

It doesn't return polygons with fill style equal to null:

new ol.style.Style({
    fill: null,
    stroke: new ol.style.Stroke({ color: "#F44444", width: 1 })
})

It worked fine using version 4.5. Please, could anybody advice, how to solve this?


Solution

  • The latest documentation (10.5.0) states this:

    getFeaturesAtPixel(pixel, options){Array}

    Get all features that intersect a pixel on the viewport. For polygons without a fill, only the stroke will be used for hit detection. Polygons must have a fill style applied to ensure that pixels inside a polygon are detected. The fill can be transparent.