I want to display different markers for different event types eg. skiing (little guy on skis), sailing (little boat). I am using views so this shouldn't be hard.

The fun starts when clustering. I want to use a marker with a the number of "sub" markers clustered. Also easy. But how to do this and then swap to the icon markers when zoomed past the clustering?

2 Answers 2


I'm not entirely sure what effect you're after, but I have a map that uses clustering and custom markers. I've added the code at the end of the answer. The code changes the marker dynamically: if there are visible markers of more than one type (taxonomy term) then it shows a general purpose marker; if all the visible markers belong to one type then it shows that type's marker. It works by creating a copy of the Style object with a custom context object that allows you to specify JS functions named after the placeholder that take the cluster as an argument and return a value to replace the placeholder. I've used the delete render intent to signify a marker's hidden and I get the feeling that might not be standard (but it works for me:). I'm not an OpenLayers guru, so no promises this is the best way!

var style_properties = settings.styles.service_providers;
var default_marker_path = style_properties.externalGraphic;
// Convert the path to an internal one. This can't be done via the
// OpenLayers interface.
// @see http://drupal.org/node/1166076#comment-5633868
default_marker_path = default_marker_path.replace(new RegExp('http(s)?://.*?' + Drupal.settings.basePath), '');

var options = {
  context: {
     * Dynamic attribute replacement function. Returns the path to the
     * appropriate marker for this cluster. It will return a specific
     * service type's marker if there's only one kind of visible marker
     * within the cluster; the default marker otherwise.
     * @param OpenLayers.Feature cluster
     *   The cluster containing the pseudo features.
     * @todo Create a TID-based index into the features array for
     *   performance benefits.
    field_service_type_marker_path: function (cluster) {

      var marker_path = default_marker_path;

      for (var i = 0; i < cluster.cluster.length; i++) {
        var feature = cluster.cluster[i];
        if (feature.renderIntent !== 'delete') {
          // Feature is visible.
          if (marker_path === default_marker_path) {
            marker_path = feature.attributes.field_service_type_marker_path;
          else if (marker_path !== feature.attributes.field_service_type_marker_path) {
            // There's more than one type of visible marker - early out
            // and use the default marker.
            marker_path = default_marker_path;
      return marker_path;
     * Checks through the features in the cluster and returns whether
     * this cluster should be visible (ie whether or not it has any
     * visible features).
    cluster_display: function (cluster) {
      for (var i = 0; i < cluster.cluster.length; i++) {
        var feature = cluster.cluster[i];
        // I could use .getVisibility(), but there's no need at the
        // moment, because the only way that a feature might not be
        // visible is if I changed its renderIntent.
        if (feature.renderIntent !== 'delete') {
          // Any return value that's not 'none' will cause the cluster
          // to be rendered.
          return '';
      return 'none';

// Set up the variable externalGraphic to allow different marker images
// based on the TID.s
style_properties.externalGraphic = Drupal.settings.basePath + '${field_service_type_marker_path}';
// Set up variable display, so that the clusters can be hidden if every
// feature inside them is.
style_properties.display = '${cluster_display}';

// Set up new styles for both 'default' and 'selected' renderIntents.
layer.styleMap.styles['default'] = new OpenLayers.Style(style_properties, options);

Checkout this post here http://drupal.org/node/1585880

It includes an example of how to get a different marker by content type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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