[GIS] Deactivating Openlayers Cluster after a certain zoom level openlayers3

openlayers

I want to disable clustering when zoom level reach to the maximum
zoom in level, I don't see any option or example how can I make it in openlayers3,

Is there any way to do it?

Best Answer

One way of disabling clustering is setting the distance (between clusters) to 0.

This example iterates all the map layers, and disables clustering at zoom level 8. It restores to my initial distance of 50 (you should use whatever initial distance is previously set; the default is 20).

map.getView().on('change:resolution', function(evt){
  var view = evt.target;

  this.getLayers().getArray().map(function(layer) {
    var source = layer.getSource();
    if (source instanceof ol.source.Cluster) {
      var distance = source.getDistance();
      if (view.getZoom() >= 9 && distance > 0) {
        source.setDistance(0);
      }
      else if (view.getZoom() < 9 && distance == 0) {
        source.setDistance(50);
      }
    }
  });
}, map);

Note that getDistance() has only been added in OpenLayers 4.1.0, albeit the code above still works without verifying the distance.

Related Question