10
votes

In OpenLayers, I have a cluster strategy to limit how many features/points the user sees on the map. When the user is fully zoomed in however, I want to turn off the clustering strategy so that all features are shown. To do this I am catching the zoom event like this:

map.events.register("zoomend", this, function (e) {
    if (map.getZoom() === this.mapMaxZoom) {
        // Don't cluster at this level. No matter what.
        this.vector.strategies[0].threshold = 1000;
        console.log("setting the clustering strategy to 1000");
    }
});

This kinda works, but I don't see the new clustering applied - I have to zoom back out again to see the clustering change to a threshold of 1000 (and thus show all features). I need some way to force openlayers to refresh. I've tried calling map.redraw() but that doesn't help. Any ideas?

9

9 Answers

14
votes

vectorlayer.refresh({force:true}); Try this.

6
votes

You should call redraw() method on layer not map - this.vector.redraw()

3
votes

I found the answer in this post. OpenLayers Cluster Recalculate

Basically, I need to set the cluster strategy, then "recluster". Works a treat.

2
votes

For openlayers 3

vectorlayer.getSource().refresh()
1
votes

None of the earlier answers worked for me. I read Open Layers 3 API and found ol.layer.Vector.changed(), which helped me. Use like: vectorLayer.changed().

1
votes

I solved the same problem thanks to this file: CenteredCluster.js, that i include after <script src="... /OpenLayers.js"></script> . I catch the file from this example: http://jorix.github.io/OL-Ragbag/examples/sundials.html for reference: https://github.com/jorix/OL-Ragbag

Then i don'`t use cluster strategy method, but the class of the file, CenteredCluster that you can set with ZoomRange options to control the behaviour of the clusters (activation, deactivation, distance and treshold):

   var centeredCluster = new OpenLayers.Strategy.CenteredCluster({
zoomSettings: [
    {zoomRange: [0, 2], settings: {distance: 10}},
    {zoomRange: [3, 4], settings: {distance: 10}},
    // 5 normal clusters
    {zoomRange: [6, 14], settings: {threshold: 2}},
    {zoomRange: [15, 99], settings: {enabled: false}}
]
});

 var urlKMLClient = 'features.kml'; 
 var layerKMLClient = new OpenLayers.Layer.Vector("Clients", {
      style : style,

     strategies: [centeredCluster, new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: urlKMLClient,
        format: new OpenLayers.Format.KML({
            extractStyles: true, 
            extractAttributes: true,
            maxDepth: 2
        })
    })
}
);
     map.addLayer(layerKMLClient);
1
votes

In OL3 refresh it by calling layer's source refresh() method

layer.getSource().refresh()

1
votes

To solve the issue just use map.updateSize();

0
votes

In OL 3.10.1 I do:

WMSLayer.getSource().updateParams({"time": Date.now()});
WFSLayer.clear();

Both layers (WMS and WFS) are refreshed successfully.

There are so many different information regarding this functionality in OL and the biggest problem is that nobody defines an OL version on which something works :)

Hopefully this will help someone!