[OpenLayers-Users] Refresh vectorLayer after de/activateclusterStrategy

Vivien Deparday vivien.deparday at gmail.com
Wed Jun 3 18:06:55 EDT 2009


Hi,
If you are interested, I had started to work on some basic toggling 
on/off of the clustering: http://trac.openlayers.org/ticket/1974
I was planning to work on it again when the 2.8 was out and when one of 
my project is done but if you have a better way to do this, you can 
submit a patch on this ticket.

Cheers,

Vivien Deparday


Heidt, Christopher M. wrote:
>
> i just extended the cluster strategy to get what i was looking for.
> Might be helpful to others.
>
> [JAVASCRIPT]
>
> //This allows me to addFeatures to my layer without deleting the 
> current ones,
> //also supports toggling activity without clearing the cache so that 
> deactivating
> //the strategy will reapply the features to the layer unclustered,
> //and activating will cluster them again
>
> OpenLayers.Strategy.AppendableCluster = 
> OpenLayers.Class(OpenLayers.Strategy.Cluster, {
>    
>     //activation now immediatly clusters
>     activate: function() {
>         var activated = OpenLayers.Strategy.prototype.activate.call(this);
>         if(activated) {
>             this.layer.events.on({
>                 "beforefeaturesadded": this.cacheFeatures,
>                 "moveend": this.cluster,
>                 scope: this
>             });
>             this.cluster(); //<--
>         }
>         return activated;
>     },
>
>     //deactivating clears clusters and adds the features directly
>     //DOES NOT CLEARCACHE
>     deactivate: function() {
>         var deactivated = 
> OpenLayers.Strategy.prototype.deactivate.call(this);
>         if(deactivated) {
>             this.layer.events.un({
>                 "beforefeaturesadded": this.cacheFeatures,
>                 "moveend": this.cluster,
>                 scope: this
>             });
>             this.layer.destroyFeatures(this.layer.features); //<--
>             this.layer.addFeatures(this.features); //<--
>         }
>         return deactivated;
>     },
>
>     //cacheFeatures now appends new features to the current cache
>     //DOES NOT CLEARCACHE
>     cacheFeatures: function(event) {
>         var propagate = true;
>         if(!this.clustering) {
>             if(this.features === null){
>                                 this.features = [];
>                         }
>                         this.features = 
> this.features.concat(event.features); //<--
>             this.cluster();
>             propagate = false;
>         }
>         return propagate;
>     },
>
>     //added an !event check to the second if to enable forced clustering
>     cluster: function(event) {
>         if((!event || event.zoomChanged) && this.features) {
>             var resolution = this.layer.map.getResolution();
>             if(!event || resolution != this.resolution || 
> !this.clustersExist()) {
>                 this.resolution = resolution;
>                 var clusters = [];
>                 var feature, clustered, cluster;
>                 for(var i=0; i<this.features.length; ++i) {
>                     feature = this.features[i];
>                     if(feature.geometry) {
>                         clustered = false;
>                         for(var j=0; j<clusters.length; ++j) {
>                             cluster = clusters[j];
>                             if(this.shouldCluster(cluster, feature)) {
>                                 this.addToCluster(cluster, feature);
>                                 clustered = true;
>                                 break;
>                             }
>                         }
>                         if(!clustered) {
>                             
> clusters.push(this.createCluster(this.features[i]));
>                         }
>                     }
>                 }
>                 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;
>                     this.layer.addFeatures(clusters);
>                     this.clustering = false;
>                 }
>                 this.clusters = clusters;
>             }
>         }
>     },
>
>     CLASS_NAME: "OpenLayers.Strategy.AppendableCluster"
> });
>
> [/JAVASCRIPT]
>
> -----Original Message-----
> From: users-bounces at openlayers.org on behalf of Heidt, Christopher M.
> Sent: Wed 6/3/2009 3:36 PM
> To: users at openlayers.org
> Subject: [OpenLayers-Users] Refresh vectorLayer after 
> de/activateclusterStrategy
>
> I use a clusterStrategy by default, but I have a button that lets the
> user toggle it off.
> Is there a way to refresh the layer so that it can redraw with/without
> clusters?
>
> I've tried:
> [JS]
> if(pressed){
>         clusterStrategy.activate();    
> }
> else{
>         clusterStrategy.deactivate();
> }
> clusterStrategy.clearCache();
> vectorLayer.redraw();
> [/JS]
>
> But that was not helpful.
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>   




More information about the Users mailing list