<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>RE: [OpenLayers-Users] Refresh vectorLayer after de/activateclusterStrategy</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>i just extended the cluster strategy to get what i was looking for.<BR>
Might be helpful to others.<BR>
<BR>
[JAVASCRIPT]<BR>
<BR>
//This allows me to addFeatures to my layer without deleting the current ones,<BR>
//also supports toggling activity without clearing the cache so that deactivating<BR>
//the strategy will reapply the features to the layer unclustered,<BR>
//and activating will cluster them again<BR>
<BR>
OpenLayers.Strategy.AppendableCluster = OpenLayers.Class(OpenLayers.Strategy.Cluster, {<BR>
<BR>
//activation now immediatly clusters<BR>
activate: function() {<BR>
var activated = OpenLayers.Strategy.prototype.activate.call(this);<BR>
if(activated) {<BR>
this.layer.events.on({<BR>
"beforefeaturesadded": this.cacheFeatures,<BR>
"moveend": this.cluster,<BR>
scope: this<BR>
});<BR>
this.cluster(); //<--<BR>
}<BR>
return activated;<BR>
},<BR>
<BR>
//deactivating clears clusters and adds the features directly<BR>
//DOES NOT CLEARCACHE<BR>
deactivate: function() {<BR>
var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);<BR>
if(deactivated) {<BR>
this.layer.events.un({<BR>
"beforefeaturesadded": this.cacheFeatures,<BR>
"moveend": this.cluster,<BR>
scope: this<BR>
});<BR>
this.layer.destroyFeatures(this.layer.features); //<--<BR>
this.layer.addFeatures(this.features); //<--<BR>
}<BR>
return deactivated;<BR>
},<BR>
<BR>
//cacheFeatures now appends new features to the current cache<BR>
//DOES NOT CLEARCACHE<BR>
cacheFeatures: function(event) {<BR>
var propagate = true;<BR>
if(!this.clustering) {<BR>
if(this.features === null){<BR>
this.features = [];<BR>
}<BR>
this.features = this.features.concat(event.features); //<--<BR>
this.cluster();<BR>
propagate = false;<BR>
}<BR>
return propagate;<BR>
},<BR>
<BR>
//added an !event check to the second if to enable forced clustering<BR>
cluster: function(event) {<BR>
if((!event || event.zoomChanged) && this.features) {<BR>
var resolution = this.layer.map.getResolution();<BR>
if(!event || resolution != this.resolution || !this.clustersExist()) {<BR>
this.resolution = resolution;<BR>
var clusters = [];<BR>
var feature, clustered, cluster;<BR>
for(var i=0; i<this.features.length; ++i) {<BR>
feature = this.features[i];<BR>
if(feature.geometry) {<BR>
clustered = false;<BR>
for(var j=0; j<clusters.length; ++j) {<BR>
cluster = clusters[j];<BR>
if(this.shouldCluster(cluster, feature)) {<BR>
this.addToCluster(cluster, feature);<BR>
clustered = true;<BR>
break;<BR>
}<BR>
}<BR>
if(!clustered) {<BR>
clusters.push(this.createCluster(this.features[i]));<BR>
}<BR>
}<BR>
}<BR>
this.layer.destroyFeatures();<BR>
if(clusters.length > 0) {<BR>
if(this.threshold > 1) {<BR>
var clone = clusters.slice();<BR>
clusters = [];<BR>
var candidate;<BR>
for(var i=0, len=clone.length; i<len; ++i) {<BR>
candidate = clone[i];<BR>
if(candidate.attributes.count < this.threshold) {<BR>
Array.prototype.push.apply(clusters, candidate.cluster);<BR>
} else {<BR>
clusters.push(candidate);<BR>
}<BR>
}<BR>
}<BR>
this.clustering = true;<BR>
this.layer.addFeatures(clusters);<BR>
this.clustering = false;<BR>
}<BR>
this.clusters = clusters;<BR>
}<BR>
}<BR>
},<BR>
<BR>
CLASS_NAME: "OpenLayers.Strategy.AppendableCluster"<BR>
});<BR>
<BR>
[/JAVASCRIPT]<BR>
<BR>
-----Original Message-----<BR>
From: users-bounces@openlayers.org on behalf of Heidt, Christopher M.<BR>
Sent: Wed 6/3/2009 3:36 PM<BR>
To: users@openlayers.org<BR>
Subject: [OpenLayers-Users] Refresh vectorLayer after de/activateclusterStrategy<BR>
<BR>
I use a clusterStrategy by default, but I have a button that lets the<BR>
user toggle it off.<BR>
Is there a way to refresh the layer so that it can redraw with/without<BR>
clusters?<BR>
<BR>
I've tried:<BR>
[JS]<BR>
if(pressed){<BR>
clusterStrategy.activate(); <BR>
}<BR>
else{<BR>
clusterStrategy.deactivate();<BR>
}<BR>
clusterStrategy.clearCache();<BR>
vectorLayer.redraw();<BR>
[/JS]<BR>
<BR>
But that was not helpful.<BR>
_______________________________________________<BR>
Users mailing list<BR>
Users@openlayers.org<BR>
<A HREF="http://openlayers.org/mailman/listinfo/users">http://openlayers.org/mailman/listinfo/users</A><BR>
<BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>