Merge pull request #709 from jorix/ghostClusters

Bug: Ghost clusters
This commit is contained in:
ahocevar
2012-10-17 10:33:30 -07:00
2 changed files with 26 additions and 3 deletions
+7 -1
View File
@@ -79,6 +79,7 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
if(activated) { if(activated) {
this.layer.events.on({ this.layer.events.on({
"beforefeaturesadded": this.cacheFeatures, "beforefeaturesadded": this.cacheFeatures,
"featuresremoved": this.clearCache,
"moveend": this.cluster, "moveend": this.cluster,
scope: this scope: this
}); });
@@ -100,6 +101,7 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
this.clearCache(); this.clearCache();
this.layer.events.un({ this.layer.events.un({
"beforefeaturesadded": this.cacheFeatures, "beforefeaturesadded": this.cacheFeatures,
"featuresremoved": this.clearCache,
"moveend": this.cluster, "moveend": this.cluster,
scope: this scope: this
}); });
@@ -134,7 +136,9 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
* Clear out the cached features. * Clear out the cached features.
*/ */
clearCache: function() { clearCache: function() {
this.features = null; if(!this.clustering) {
this.features = null;
}
}, },
/** /**
@@ -169,7 +173,9 @@ OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
} }
} }
} }
this.clustering = true;
this.layer.removeAllFeatures(); this.layer.removeAllFeatures();
this.clustering = false;
if(clusters.length > 0) { if(clusters.length > 0) {
if(this.threshold > 1) { if(this.threshold > 1) {
var clone = clusters.slice(); var clone = clusters.slice();
+18 -1
View File
@@ -19,7 +19,7 @@
} }
function test_clusters(t) { function test_clusters(t) {
t.plan(17); t.plan(22);
function featuresEq(got, exp) { function featuresEq(got, exp) {
var eq = false; var eq = false;
@@ -104,6 +104,23 @@
t.eq(layer.features.length, features.length-1, "[1-threshold 100] layer has " + (features.length-1) + " clusters"); 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"); 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) { function test_deactivate(t) {