Search code examples
javascriptopenlayersopenlayers-5

OpenLayers5: hitTolerance doesn't seem to work with WMS layer


I am new to OpenLayers as I just today tried porting over my map from Leaflet to OL since hitTolerance is exactly what I was looking for. Also, I understand v5 is quite new, so the docs are not completely updated in my experience.

I can't seem to get hitTolerance to work with WMS layers.

I understand that since v5 you can now specify hitTolerance in forEachLayerAtPixel, but I still have to hit the layer pretty accurately for it to be picked up by forEachLayerAtPixel.

I also tried forEachFeatureAtPixel, but that gave me nothing, ever. Does WMS layers have features?

I have tried both with TileLayer/TileWMS and ImageLayer/ImageWMS. Both behave the same. Using forEachLayerAtPixel even with a hitTolerance of 100 I still have to be very precise to hit the layer.

Am I doing something wrong? I don't know if this is a bug, or if hitTolerance simply doesn't work with WMS layers. Any help would be appreciated. Thanks.


Solution

  • The hitTolerance applies to vector data. OL takes your click coordinates and searches for nearby features.

    When dealing with WMS, OL has no knowledge of the features, it is just displaying an image. It just sends the click coordinates to the map server, which may or not apply a buffer to it, and return the found features info.

    So, for WMS, the hit tolerance is a server setting, not a client one.

    For example, here is the corresponding doc for Geoserver.