0

I have a vector layer with clustering in OL3.4:

cluster = new ol.layer.Vector({
          source: new ol.source.Cluster({
            distance: 20,
            source: some_geojson
          }),
          style: some_style,
});

I want to change the cluster 'distance' property, after some event, particularly:

map.getView().on('change:resolution',function(){
    // change cluster distance here (and refresh it)
}

What is the best approach to do this?

1

Currently you can't directly change the distance property on an ol.source.Cluster.

You've 2 solutions I think:

1) first idea: on change:resolution event set a new source to your layer:

map.getView().on('change:resolution',function(){ cluster.setSource(new ol.source.Cluster({ distance: 40, source: some_geojson }) );

2) Defines multiple layers which are displayed at different resolutions:

cluster1 = new ol.layer.Vector({
  minResolution: 200,
  maxResolution: 2000
  source: new ol.source.Cluster({
    distance: 20,
    source: some_geojson
  }),
  style: some_style,
});
cluster2 = new ol.layer.Vector({
  minResolution: 2000,
  maxResolution: 20000
  source: new ol.source.Cluster({
    distance: 40,
    source: some_geojson
  }),
  style: some_style,
});

Hope this helps

1

This is an old thread, but if anyone stumbles upon this in the future, this is a working example of clustered features with variable distance based on the slider shown:

https://openlayers.org/en/latest/examples/cluster.html

Specifically, the code to set the distance is clusterSource.setDistance(distance). This is how it's used in the example above, using a slider called distance:

distance.addEventListener('input', function() { clusterSource.setDistance(parseInt(distance.value, 10)); });

(OpenLayers 4.3.1, working as of 8/16/2017)

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.