1

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?

4

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.

1

You can also listen to:

map.getView().on('change:resolution', function(evt){
    //according to http://openlayers.org/en/v3.5.0/apidoc/ol.View.html
    // I think this is not true for any scenario
    //40075016.68557849 / 256 / Math.pow(2, 28) = 0.0005831682455839253


    var resolution = evt.target.get(evt.key),
        resolution_constant = 40075016.68557849,
        tile_pixel = 256;

    var result_resol_const_tile_px = resolution_constant / tile_pixel / resolution;

    var currentZoom = Math.log(result_resol_const_tile_px) / Math.log(2);


    console.info(currentZoom, resolution);

});

I'm converting resolution to zoom just for curiosity.

  • thanks for sharing but it is not related to my question.. – talsibony Jun 4 '15 at 4:31
  • 1
    Well, it is just missing the if (zoom > 14 && Cluster) { part. And moreover, if you want to check zoom level, this is with ('change:resolution', callback). But it is up to you. – Jonatas Walker Jun 4 '15 at 9:48
1

I found a way doing it but I am not sure it is the right way, it works I will love to get your feedback

on map move end I am checking the current zoom and if it is larger than the max zoom level I am changing the layer source to none cluster source

var Cluster = true;
map.on('moveend', function (e) {
    var zoom = e.map.getView().getZoom();
    if (zoom > 14 && Cluster) {
        // do remove the cluster layer
        arrowsLayer.setSource(routesSource);
        Cluster = false;
    } else {
        if (!Cluster) {
            arrowsLayer.setSource(routesClusterSource);
            Cluster = true;
        }
    }
});

is there a way to remain with the cluster source and tell it to avoind clustering in some condition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.