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);
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | leaflet-cluster-group-markers-3257616-5.patch | 737 bytes | davidhk |
Comments
Comment #3
itamair commentedthanks @davidhk
your proposed changes have been added into dev branch, will be part of the next Leaflet release.
Comment #4
itamair commentedComment #5
davidhk commentedAfter 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.
Comment #7
itamair commentedwell ... 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 ...
Comment #8
davidhk commentedI 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.