11

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?

Matt Roberts
  • 26,371
  • 31
  • 103
  • 180

9 Answers9

15

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

Kliver Max
  • 5,107
  • 22
  • 95
  • 148
6

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

igorti
  • 3,828
  • 3
  • 22
  • 29
  • Thanks, but I tried that too, and it didn't work (although I think it should!) – Matt Roberts Apr 27 '12 at 11:18
  • 1
    I'm not sure, but you could also try to call refresh() method on layer. I would also try calling deactivate() method on strategy instead of setting high treshhold - this.vector.strategies[0].deactivate(). You can also try to listen to map's "movestart" instead of "zoomend". I think features are not drawn yet on movestart so you have better chance to prevent things from happening. – igorti Apr 27 '12 at 11:39
5

For openlayers > 3

vectorlayer.getSource().refresh()
Argiropoulos Stavros
  • 9,436
  • 11
  • 61
  • 79
3

I found the answer in this post. OpenLayers Cluster Recalculate

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

Community
  • 1
  • 1
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180
2

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().

Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
1

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);
sasi kanth
  • 2,637
  • 4
  • 17
  • 29
1

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

layer.getSource().refresh()

zeuros
  • 11
  • 1
1

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

Khem Raj Regmi
  • 2,130
  • 19
  • 21
0

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!

marian0
  • 3,336
  • 3
  • 27
  • 37