Search code examples
javascriptopenlayersmarkerclusterer

OpenLayers, nice marker clustering


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


Solution

  • 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,
      // ... 
    });