gpt4 book ai didi

javascript - 如何在单击 Leaflet map 时更改群集缩放级别?

转载 作者:行者123 更新时间:2023-11-29 21:41:57 25 4
gpt4 key购买 nike

我有一个缩放级别为 2-7 并使用 MarkerCluster 插件的传单 map ,默认情况下我有 L.MarkerClusterGroup 禁用缩放级别 2 的聚类(这意味着没有聚类)并且我试图允许用户单击一个按钮,然后将聚类缩放级别更改为 5。这可能吗?

我知道我可以通过创建两个 markercluster 组来做到这一点,一个没有聚类,一个有聚类并根据点击删除/添加它,但这看起来非常困惑。真的,有几种方法可以做到这一点,但它们非常笨拙。

代码:

默认(2 是最低级别的缩放):

var markers = new L.MarkerClusterGroup (
{
disableClusteringAtZoom: 2,
maxClusterRadius: 100,
animateAddingMarkers: true
});

我想做的能做的:

 $('#mcluster').click(function() {
//do some code that sets the disableClusterAtZoom to 5
});

最佳答案

我找不到在缩放时禁用集群或为 disableClustering 设置新值的方法,但我找到了一种不太笨拙的方法来实现这一点。

var markers = new L.LayerGroup(); //non cluster layer is added to map
markers.addTo(map);
var clusters = new L.MarkerClusterGroup (
{
disableClusteringAtZoom: 5,
maxClusterRadius: 100,
animateAddingMarkers: true
}); //cluster layer is set and waiting to be used

var clusterStatus = 'no'; //since non cluster group is on by default, the status for cluster is set to no
$('#mcluster').click(function( event ) {
if(clusterStatus === 'no'){
clusterStatus = 'yes';
var current1 = markers.getLayers(); //get current layers in markers
map.removeLayer(markers); // remove markers from map
clusters.clearLayers(); // clear any layers in clusters just in case
current1.forEach(function(item) { //loop through the current layers and add them to clusters
clusters.addLayer(item);
});
map.addLayer(clusters);
} else {
clusterStatus = 'no'; //we're turning off clustering here
var current2 = clusters.getLayers(); //same code as before just reversed
map.removeLayer(clusters);
markers.clearLayers();
current2.forEach(function(item) {
markers.addLayer(item);
});
map.addLayer(markers);
}
});

我确信有一个更优雅的解决方案,但随着我不断增长的知识,这就是我想出的。

关于javascript - 如何在单击 Leaflet map 时更改群集缩放级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com