Problem/Motivation

In our D7 site I submit all the markers in a group so they appear as a layer in the layer switcher that the user can turn on / off.

Using D9 with Leaflet 2.x.19, that works if I just use the Leaflet module, but once I enable the Leaflet MarkerCluster module there are two problems:

A. - The group layer isn't shown in the layer switcher

B. - The markers in the group are not clustered

Steps to reproduce

The code that builds the array of markers / points that are passed in to leafletRenderMap() ends:

    // We used to do:
    //   return $points;
    // Instead, return them as a group.
    // Then they're displayed as a layer in the layer switcher so
    // so they can be toggled on & off
    $group_of_points[] = array(
      'group' => true,
      'features' => $points,
      'label' => 'Markers',
    );

Proposed resolution

A. - The group layer isn't shown in the layer switcher

The code in leaflet.drupal.js that works is at line 247:

// Add the group to the layer switcher.
 self.add_overlay(feature.label, lGroup, false, mapid);

The code in leaflet_markercluster.drupal.js that doesn't work is at line 35:

// @todo we need to correctly handle the groups here
 cluster_layer.addLayer(lGroup);

I changed it to use the code from leaflet.drupal.js and now the group layer is shown in the layer switcher. ('self' isn't defined, so I used 'this' instead)

        // Add the group to the layer switcher.
        this.add_overlay(feature.label, lGroup, false, mapid);

B. - The markers in the group are not clustered

The code to handle groups in leaflet_markercluster.drupal.js at line 19 creates a regular layer for all the points in the group:

var lGroup = this.create_feature_group(feature);

Changing that to use a clustering layer shows the markers clustered correctly:

var lGroup = new L.MarkerClusterGroup(leaflet_markercluster_options);

Comments

davidhk created an issue. See original summary.

  • itamair committed f420c5f on 2.1.x
    leaflet_markercluster.drupal.js refactoring (also 'Issue #3257616: Group...
itamair’s picture

thanks @davidhk
your proposed changes have been added into dev branch, will be part of the next Leaflet release.

itamair’s picture

Status: Active » Fixed
davidhk’s picture

Version: 2.1.x-dev » 2.2.1
Status: Fixed » Active
StatusFileSize
new737 bytes

After upgrading Leaflet to the latest 2.2.1 I found that the group now appears in the layer switcher (thank you!) but the markers in the group still aren't clustered.

I made the edit 'B' described above, and the markers are clustered again. Please could that edit also be added to the module?

Patch attached.

  • itamair committed e1aae3a on 2.2.x
    Issue #3257616 by davidhk, itamair: Group of features not working with...
itamair’s picture

Status: Active » Fixed

well ... I am driving blind here, and simply trusting your patches (though also this one didn't apply cleanly and I had to hack manually),
because I don't have any reproducing scenario,
Hope your fix works and also that all the community will be grateful to you ...
Thanks @davidhk. Going to deploy a new Leaflet release with this further fix ...

davidhk’s picture

I confirm that the group of markers is clustered correctly with v2.2.2.

Thank you for the fast turnaround, and for taking care of my bad patch. Maps are a key part of the website I run, so I appreciate all the work you put into maintaining and enhancing this module.

Status: Fixed » Closed (fixed)

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