Skip to content

Commit

Permalink
Merge pull request openlayers#709 from jorix/ghostClusters
Browse files Browse the repository at this point in the history
Bug: Ghost clusters
  • Loading branch information
ahocevar committed Oct 17, 2012
2 parents 788d49a + ee34448 commit 4b7a0b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/OpenLayers/Strategy/Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
if(activated) {
this.layer.events.on({
"beforefeaturesadded": this.cacheFeatures,
"featuresremoved": this.clearCache,
"moveend": this.cluster,
scope: this
});
Expand All @@ -100,6 +101,7 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
this.clearCache();
this.layer.events.un({
"beforefeaturesadded": this.cacheFeatures,
"featuresremoved": this.clearCache,
"moveend": this.cluster,
scope: this
});
Expand Down Expand Up @@ -134,7 +136,9 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
* Clear out the cached features.
*/
clearCache: function() {
this.features = null;
if(!this.clustering) {
this.features = null;
}
},

/**
Expand Down Expand Up @@ -169,7 +173,9 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
}
}
}
this.clustering = true;
this.layer.removeAllFeatures();
this.clustering = false;
if(clusters.length > 0) {
if(this.threshold > 1) {
var clone = clusters.slice();
Expand Down
21 changes: 19 additions & 2 deletions tests/Strategy/Cluster.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

function test_clusters(t) {
t.plan(17);
t.plan(22);

function featuresEq(got, exp) {
var eq = false;
Expand Down Expand Up @@ -103,7 +103,24 @@
// the one feature with no geometry is not added to the layer
t.eq(layer.features.length, features.length-1, "[1-threshold 100] layer has " + (features.length-1) + " clusters");
t.ok(featuresEq(layer.features, features.slice(0, 80)), "[1-threshold 100] layer has all features with geometry");


// remove features and zoom
strategy.threshold = 1;
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
t.eq(strategy.features.length, 81,
"[remove features] cluster has cache");
layer.removeAllFeatures();
t.eq(layer.features.length, 0,
"[remove features] layer has no features after remove");
t.ok(!strategy.features,
"[remove features] cluster has no cache after remove");
map.zoomIn();
t.eq(layer.features.length, 0,
"[remove features] layer has no features after zoom");
t.ok(!strategy.features,
"[remove features] cluster has no cache after zoom");

map.destroy();
}

function test_deactivate(t) {
Expand Down

0 comments on commit 4b7a0b4

Please sign in to comment.