OpenLayers, nice marker clustering

apneadiving picture apneadiving · Jul 10, 2011 · Viewed 27.4k times · Source

Do you know how to have a nice clustering in OpenLayers such as this google example ?

Answer

unibasil picture unibasil · Aug 3, 2012

You can add label to pointStyle in above example and explain context of this label. Your code should be something like this:

var pointStyle = new OpenLayers.Style({
    // ...
    'label': "${label}",
    // ...
  }, {
    context: {
      // ...
      label: function(feature) {
        // clustered features count or blank if feature is not a cluster
        return feature.cluster ? feature.cluster.length : "";  
      }
      // ..
    }
});

var styleMap = new OpenLayers.StyleMap({
  'default': pointStyle,
});

var googleLikeLayer = new OpenLayers.Layer.Vector("GoogleLikeLayer", {
  // ...
  styleMap  : styleMap,
  // ... 
});