diff --git a/src/ol/source/Cluster.js b/src/ol/source/Cluster.js index 5c19ca3ab6..11b6affd64 100644 --- a/src/ol/source/Cluster.js +++ b/src/ol/source/Cluster.js @@ -38,6 +38,8 @@ import {getUid} from '../util.js'; * return feature.getGeometry(); * } * ``` + * @property {function(Feature, Array):any} [hydrate] Function called after creating + * cluster feature * See {@link module:ol/geom/Polygon~Polygon#getInteriorPoint} for a way to get a cluster * calculation point for polygons. * @property {VectorSource} [source] Source. @@ -108,6 +110,12 @@ class Cluster extends VectorSource { return geometry; }; + /** + * @type {function(Feature, Array):any} + * @protected + */ + this.hydrate = options.hydrate; + /** * @type {VectorSource} * @protected @@ -295,6 +303,9 @@ class Cluster extends VectorSource { centroid[1] * (1 - ratio) + searchCenter[1] * ratio, ]); const cluster = new Feature(geometry); + if (this.hydrate) { + this.hydrate(cluster, features); + } cluster.set('features', features, true); return cluster; } diff --git a/test/browser/spec/ol/source/cluster.test.js b/test/browser/spec/ol/source/cluster.test.js index 3b11e384eb..655381b802 100644 --- a/test/browser/spec/ol/source/cluster.test.js +++ b/test/browser/spec/ol/source/cluster.test.js @@ -75,6 +75,23 @@ describe('ol.source.Cluster', function () { expect(source.getFeatures().length).to.be(1); expect(source.getFeatures()[0].get('features').length).to.be(2); }); + it('hydrates cluster feature with additional fields', function () { + const feature1 = new Feature(new Point([0, 0])); + const feature2 = new Feature(new Point([0, 0])); + feature1.set('value', 1); + feature2.set('value', 2); + const source = new Cluster({ + source: new VectorSource({ + features: [feature1, feature2], + }), + hydrate: function (clusterFeature, features) { + clusterFeature.set('sum', 3); + }, + }); + source.loadFeatures(extent, 1, projection); + expect(source.getFeatures().length).to.be(1); + expect(source.getFeatures()[0].get('sum')).to.be(3); + }); }); describe('#setDistance', function () {