I want to create clusters on map with numbers in it. I have search for it but could not find a right path. I figured out that i can do this with dynamic styling but cant figure out how. Help needed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joey-santiago’s picture

i'm trying to get the same, but actually with some more features too. I'd like to create something like this: http://acuriousanimal.com/code/animatedCluster/

at the moment i tried hacking the openlayers_behavior_cluster.js file and i could get the plugin working, but i can't find the way to modify the style of the layer from there. When i'm ready i could then create a new behavior in a new module and probably submit it.

Can anyone help me out understanding how can i add a new vector to the map putting in there map's features and using a style for it?

// Create a Style that uses the three previous rules
            var style = new OpenLayers.Style(null, {
                rules: [lowRule, middleRule, highRule]
            });
var vector2 = new OpenLayers.Layer.Vector("Features", {
                renderers: ['Canvas','SVG'],
                strategies: [
                    new OpenLayers.Strategy.AnimatedCluster({
                        distance: 45,
                        animationMethod: OpenLayers.Easing.Expo.easeOut,
                        animationDuration: 20
                    })
                ],
                styleMap:  new OpenLayers.StyleMap(style)
            });
            map.addLayer(vector2);

thanks

joey-santiago’s picture

Wow!

i found a way to use this clustering method. I had to hack also the file openlayers.views.vector.js.

Here's my version of the files:
openlayers.behavior.cluster.inc . just added this line at 90.

drupal_add_js(drupal_get_path("module", "custom") . "/js/animatedcluster.js");

openlayers.behavior.cluster.js modified the addBehavior with this (basically just extended the options and used a different strategy to cluster)

Drupal.openlayers.addBehavior('openlayers_behavior_cluster', function (data, options) {
  
  options.animationMethod = OpenLayers.Easing.Expo.easeOut;
  options.animationDuration = 10;
            
  var map = data.openlayers;
  var distance = parseInt(options.distance, 10);
  var threshold = parseInt(options.threshold, 10);
  var layers = [];
  for (var i in options.clusterlayer) {
    var selectedLayer = map.getLayersBy('drupalID', options.clusterlayer[i]);
    if (typeof selectedLayer[0] != 'undefined') {
      layers.push(selectedLayer[0]);
    }
  }

  // Go through chosen layers
  for (var i in layers) {
    var layer = layers[i];
    // Ensure vector layer
    if (layer.CLASS_NAME == 'OpenLayers.Layer.Vector') {
      //var cluster = new OpenLayers.Strategy.Cluster(options);
      var cluster =  new OpenLayers.Strategy.AnimatedCluster(options);

      layer.addOptions({ 'strategies': [cluster] });
      cluster.setLayer(layer);
      cluster.features = layer.features.slice();
      cluster.activate();
      cluster.cluster();
    }
  }
});

openlayers.views.vector.js: Used the styles i wanna use to the map at line 90

options.options.styleMap =  new OpenLayers.StyleMap(style);

hope it helps you :)

it would be great to make this as a new module... could someone give me some hints on how to add this functionality without interfering with module's code?

Mujtaba Ahmed’s picture

Nice work. I want to make one cluster for all layers to make map neat. Can you help ?

_wdm_’s picture

I have created a sandbox project for this:
http://drupal.org/sandbox/william.mcrae/1815030

mellowtothemax’s picture

Hi,

Thank you all for taking the time to provide this feature.

I am having trouble with it though. The map shows up empty when I enable the animated cluster behaviour. Could it be conflicting with other behaviours or modules?

Currently i am using _wdm_'s sandbox.

I also have openlayers plus installed.

regards,

_wdm_’s picture

Hi mellowtothemax,

Did you install sites/all/libraries/AnimatedCluster/AnimatedCluster.js ?
Otherwise try disabling all the other behaviours to see what happens.

regards.

mellowtothemax’s picture

Hey _wdm_,

Thanks for the reply. Yes, AnimatedCluster.js is in the correct location. I have also disabled all behaviours and uninstalled openlayers plus. The points are diplayed now (after removing openlayers plus) but unfortunatelly the animated feature is still not working.

regards,

Pol’s picture

Status: Active » Closed (works as designed)

Hello,

Can you try with the latest dev release and report back ?
Re open the ticket if needed.

Thanks !

ditcheva’s picture

Status: Closed (works as designed) » Needs work

@Pol, I also had some problems getting _wdm_'s module to work, and also find it would be confusing to download that extra helper module and have two 'cluster' sections in the OpenLayer's behavior UI. One that module introduces and one that's openlayer's own cluster sections. When I was testing it out, I was wondering whether one would cancel out the other, or are they supposed to work together.. I think it's a little odd.

Since Openlayers already has a 'cluster' strategy in its default behavior's list, would you be open to a patch that takes the code both joey-santiago and _wdm_ have worked on and applies that patch to openlayer's own clustering strategy?

I think that would be better and would be willing to create it with both options for cluster sizes and cluster labels....

There are many, many tickets asking for both.

ditcheva’s picture

Status: Needs work » Needs review
FileSize
47.53 KB
487.46 KB
552.48 KB

Well here is the patch for the following new functionality in the default Openlayers clustering behavior, based on _wdm_'s sandbox module, with a bug fix which got it working for me and a couple of new features and handling of individual features.

1. Three levels of cluster sizes (you choose the cluster number boundaries for each)
2. Color choices for the different cluster sizes
3. User gets to choose whether to put the number of points in each cluster as a label via UI checkbox
4. Individual features (which are not clustered together) are treated differently, and have the style you applied to the layer applied to them. Yay!

I'm attaching screenshots of the 'before' clustering functionality in Openlayers, the 'after' and the new UI options.

Let me know if this works for everyone. I'd love to see it added to the module!!!

Pol’s picture

Guys, gals,

I think it's an excellent job, I will test it out tomorrow probably, but I love the screenshots already.

Sadly, this will require the inclusion of a third party library, so it cannot go in OpenLayers, the module.
I'm of course, ok to create a real module from this, I think it would help a lot of people, including me, almost alone to maintain OpenLayers.

Waiting for your feedback on this.

ditcheva’s picture

And here's the actual patch. Ooops.

You know, it doesn't require a third library actually. Just adds on to the already existing clustering behavior, but adds beautiful *styling* to it. :-)

ditcheva’s picture

And I should say, @Pol, that you're doing an awesome job supporting this very complicated and used module, and your promptness certainly makes people very willing to use it and contribute to it!

Pol’s picture

Assigned: Unassigned » Pol

Many thanks !

If my test of tonight are successful, this goes in :-) I'm happy that someone is actually working on this part !

I also did a small module that I need to update: http://drupal.org/project/openlayers_heatmaps
You can review it too if you want.

Pol’s picture

Status: Needs review » Fixed

I removed the trailing spaces ;)
Beside that, excellent, committed :-)
Thanks !

ditcheva’s picture

Status: Fixed » Needs review
FileSize
1.4 KB

The good thing about actually *using* the functionality you're trying to add to a module is that you realize very quickly (before anyone else) if you have missed something!

So here's a patch to my patch. In the previous version, the styling rules/changes I added did not apply to any features left in the layer that aren't clusters. The reason I never noticed is because I never set or used the 'threshold' variable in the UI which specifies the number of features in a cluster below which you want to add those individually to the layers versus in a cluster.

Since I never set that threshold, every one of my points was in a cluster, even when they were clusters of 1! And because they were all clusters, they were styled properly and appeared on the map.

However, now that I've started testing out setting the threshold, I realize that my rules do not catch, style and display non-clustered features, when a user does set a threshold.

The following patch corrects that!

I hope the above makes sense. I've tried to describe it. In any case, from my own usage and testing of these new styles to clusters, this completes the new cluster styling rules to may satisfaction, and I am even getting ready to roll them all out to my production site. Let me know if you all agree with this addition too.

Pol’s picture

Thanks, committed !

planctus’s picture

Cool...but, is there any chance to use background images instead of background colors for this clusters..?

Status: Needs review » Needs work

The last submitted patch, openlayers_clusters_styling_non-clustered_features_1677522.patch, failed testing.

Pol’s picture

Status: Needs work » Fixed

Planctus: No it's not possible.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

AaronBauman’s picture

What's the best way to plug in animatedCluster strategy, mentioned in the first few comments above?
Should I open a new support request issue for discussion?

ditcheva’s picture

Clustering as shown in the 'after' screenshot in #10 has already been rolled into the latest release. Update to the latest version, and clustering should be a behavior you can turn on for your map...

AaronBauman’s picture

Yes, I see that basic clustering is now included with the latest release.
I'm asking specifically about animatedCluster strategy, as mentioned in the first couple comments of this thread.

ditcheva’s picture

Ah, got it. Sorry. I think incorporating the animated Cluster strategy that was initially discussed required using additional external library files, and that's why it was decided not to go into that direction... That functionality is not currently available in the drupal OpenLayers mapping functionality.

I'm not sure if that would be reconsidered, but wanted to share what I remember from the original discussion... Maybe one of the module maintainers can chime in too.

AaronBauman’s picture

OK, thanks for the recap.
If I really need it, i'll investigate writing it as a strategy plugin.

filburt’s picture

Hi,

I updated the module and now I have a different cluster behavior than before: I'd like to have the same icon for the clustered points as for a single point - this was the behavior in former versions of this module and it is a bit frustrating that it seams not to work anymore. (Downgrade to the old module version fails.)

My usecase: I have points for projects in various regions. If I zoom out I'd like to cluster the projects which are located nearby - but I still want to have the same marker...

Since I have two layers with different markers it makes no sense to style the cluster markers, because there no possibility to distinguish the cluster markers for different layers.

Is there any solution to get the old behavior? #18 requested it, too. An idea for example would be a checkbox "Use default layer marker for clusters"

Thanks a lot!
Filburt