diff --git a/changelog/upgrade-notes.md b/changelog/upgrade-notes.md index ccd01b4e71..32f8df8397 100644 --- a/changelog/upgrade-notes.md +++ b/changelog/upgrade-notes.md @@ -279,6 +279,10 @@ The `getGetFeatureInfoUrl` of `ol/source/ImageWMS` and `ol/source/TileWMS` is no The `SelectInteraction` is removed. There are two examples ([Select Features by Hover](https://openlayers.org/en/master/examples/select-hover-features.html) and [Select multiple Features](https://openlayers.org/en/master/examples/select-multiple-features.html) which show how similar results can be achieved by using more basic methods. +##### `getFeatureAtPixel` returns always an array + +`getFeatureAtPixel` returns now an empty array instead of `null` if no features were found. + #### Other changes ##### Allow declutter in image render mode diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 82286f3709..a520636521 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -562,15 +562,12 @@ class PluggableMap extends BaseObject { * @param {import("./pixel.js").Pixel} pixel Pixel. * @param {AtPixelOptions=} opt_options Optional options. * @return {Array} The detected features or - * `null` if none were found. + * an empty array if none were found. * @api */ getFeaturesAtPixel(pixel, opt_options) { - let features = null; + const features = []; this.forEachFeatureAtPixel(pixel, function(feature) { - if (!features) { - features = []; - } features.push(feature); }, opt_options); return features; diff --git a/test/spec/ol/map.test.js b/test/spec/ol/map.test.js index 1d97f486cc..3ad872aa87 100644 --- a/test/spec/ol/map.test.js +++ b/test/spec/ol/map.test.js @@ -279,9 +279,10 @@ describe('ol.Map', function() { document.body.removeChild(target); }); - it('returns null if no feature was found', function() { + it('returns an empty array if no feature was found', function() { const features = map.getFeaturesAtPixel([0, 0]); - expect(features).to.be(null); + expect(features).to.be.an(Array); + expect(features).to.be.empty(); }); it('returns an array of found features', function() { @@ -311,10 +312,11 @@ describe('ol.Map', function() { map.addLayer(otherLayer); const features = map.getFeaturesAtPixel([50, 50], { layerFilter: function(layer) { - return layer == otherLayer; + return layer === otherLayer; } }); - expect(features).to.be(null); + expect(features).to.be.an(Array); + expect(features).to.be.empty(); }); }); diff --git a/test/spec/ol/renderer/canvas/vectortilelayer.test.js b/test/spec/ol/renderer/canvas/vectortilelayer.test.js index d20bc0715a..ef11208820 100644 --- a/test/spec/ol/renderer/canvas/vectortilelayer.test.js +++ b/test/spec/ol/renderer/canvas/vectortilelayer.test.js @@ -369,7 +369,8 @@ describe('ol.renderer.canvas.VectorTileLayer', function() { const features = map.getFeaturesAtPixel([96, 96]); document.body.removeChild(target); map.dispose(); - expect(features).to.be(null); + expect(features).to.be.an(Array); + expect(features).to.be.empty(); done(); }, 200); });