function zoom to cluster 关闭同时具有多个标记的集群 lat/lng

function zoom to cluster closes cluster with multiple markers at same lat/lng

我正在使用传单创建地图,其中包含标记簇和标记簇图层支持插件。

我有一个带有多个叠加层的图层控件,因此其中一些选中的标记会在相同 lat/lng 时弹出,但数据不同。

这些点正确地聚类,但我遇到了一个问题,如果我在同一 lat/lng 上单击一组点,则该聚类不会居中,因此通常会偏向一边并且很难看到,除非您手动单击并拖动以将其居中。

如果我单击具有不同 lat/lng 点的集群,它会放大并居中到该区域。为了尝试解决这个问题,我添加了这 2 个函数,一个单独应用于标记,以便在单击它们时它们位于地图的中心,一个应用于被单击的集群,这会导致地图缩放到集群的边界。

当您单击集群时,它会展开并在同一点显示标记,但随后它会缩放集群并将其居中并关闭集群,因此您必须再次单击它显示标记。我不希望这发生。

我在想也许使用 panTo 的函数在群集点击时效果更好?但不确定如何实施。谢谢! 这些是函数:

    markerClusters.on('clusterclick', function (a) {
        a.layer.zoomToBounds();
    });


    map.on('popupopen', function(e) {
var px = map.project(e.popup._latlng); // find the pixel location on the map where the popup anchor is
px.y -= e.popup._container.clientHeight/2 // find the height of the popup container, divide by 2, subtract from the Y axis of marker location
map.panTo(map.unproject(px),{animate: true}); // pan to new center

    });

您可能应该在 markerClusters MCG 上使用 "spiderfied" 事件,而不是 "clusterclick" 事件,后者也会为不需要 spiderfy 的集群触发。

此外,正如您发现的那样,一旦执行缩放,该操作就会关闭 spiderfication。如您所料,使用 panTo 可以避免缩放更改,因此不会关闭 spiderfication。

markerClusters.on('spiderfied', function (event) {
  var cluster = event.cluster;

  // Center the map on the clicked cluster if it gets spiderfied.
  map.panTo(cluster.getLatLng());
});

演示:https://jsfiddle.net/3v7hd2vx/397/