Adding a threshold property to the cluster strategy. If a threshold set, clusters will only be created if the number of features in a group meets or exceeds the threshold number. r=elemoine,crschmidt (closes #1815)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9119 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-23 17:34:28 +00:00
parent 85dc3a9880
commit 3e55ef6aa3
3 changed files with 193 additions and 11 deletions

View File

@@ -22,6 +22,17 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
*/
distance: 20,
/**
* APIProperty: threshold
* {Integer} Optional threshold below which original features will be
* added to the layer instead of clusters. For example, a threshold
* of 3 would mean that any time there are 2 or fewer features in
* a cluster, those features will be added directly to the layer instead
* of a cluster representing those features. Default is null (which is
* equivalent to 1 - meaning that clusters may contain just one feature).
*/
threshold: null,
/**
* Property: features
* {Array(<OpenLayers.Feature.Vector>)} Cached features.
@@ -122,15 +133,9 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
/**
* Method: clearCache
* Clear out the cached features. This destroys features, assuming
* nothing else has a reference.
* Clear out the cached features.
*/
clearCache: function() {
if(this.features) {
for(var i=0; i<this.features.length; ++i) {
this.features[i].destroy();
}
}
this.features = null;
},
@@ -168,6 +173,19 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
}
this.layer.destroyFeatures();
if(clusters.length > 0) {
if(this.threshold > 1) {
var clone = clusters.slice();
clusters = [];
var candidate;
for(var i=0, len=clone.length; i<len; ++i) {
candidate = clone[i];
if(candidate.attributes.count < this.threshold) {
Array.prototype.push.apply(clusters, candidate.cluster);
} else {
clusters.push(candidate);
}
}
}
this.clustering = true;
// A legitimate feature addition could occur during this
// addFeatures call. For clustering to behave well, features