gpt4 book ai didi

javascript - 防止多个 markerClusterGroup 图标在 Leaflet 中重叠

转载 作者:行者123 更新时间:2023-11-30 14:22:59 25 4
gpt4 key购买 nike

我有两个偶尔重叠的独立 markerClusterGroup。有什么办法可以防止这种情况发生吗?在我的实际代码中,我为其中一个集群组使用自定义图标,以便我可以区分这两种集群类型。但是,对于此示例而言,这不是必需的,因此为了简单起见,我省略了该部分。

var map = L.map("map");

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

map.setView([48.85, 2.35], 12);
var mcg = L.markerClusterGroup().addTo(map);
var mcg2 = L.markerClusterGroup().addTo(map);

L.marker([48.85, 2.35]).addTo(mcg);
L.marker([48.85, 2.34]).addTo(mcg);

for(var i=0;i<40;i++){
L.marker([48.85, 2.34091]).addTo(mcg2);
}

这是我的意思的一个例子:

http://plnkr.co/edit/yqIhI7RMsp9A7I3AwGnY?p=preview

Screenshot

要求指出类别 1 中的标记必须与类别 2 中的标记分开聚类。但是这两种类型必须同时显示在 map 上。

最佳答案

Is there anyway to prevent this?

不像你那样有几个 Leaflet.markercluster 组。

想一想:当一个给定的组没有关于另一个组的数据时,应该在哪里计算聚类图标的位置?

您可能有几种可能的解决方法和/或其他可能更适合您需要的库,而不必自己重新编写聚类算法。

例如,在聚类时显示不同类别的流行替代方法是 PruneCluster插件:

PruneCluster is a fast and realtime marker clustering library.

It's working with Leaflet as an alternative to Leaflet.markercluster.

Screenshot of map with PruneCluster icon categories摘自PruneCluster home page

另一种可能的解决方法是将所有类别合并到同一个标记集群组中,但使用后者的集群图标 customized以便它们呈现与上面的 PruneCluster 屏幕截图类似,甚至为每个类别呈现假图标:

Screenshot of Leaflet.markercluster with customized cluster icons for each category

function customClusterIcon(cluster) {
// Count number of markers from each category.
var markers = cluster.getAllChildMarkers();
var cat1count = 0;
var cat2count = 0;
for (var marker of markers) {
var category = marker.options.category;
if (category && category === 'cat2') {
cat2count += 1;
} else {
cat1count += 1;
}
}
// Generate the cluster icon depending on presence of Markers from different categories.
if (cat2count === 0) {
return L.divIcon({
html: cat1count,
className: 'cat1cluster cluster',
iconSize: [20, 20]
});
} else if (cat1count === 0) {
return L.divIcon({
html: cat2count,
className: 'cat2cluster cluster',
iconSize: [20, 20]
});
} else {
return L.divIcon({
html: `
<div class="cat1cluster cluster">${cat1count}</div>
<div class="cat2cluster cluster">${cat2count}</div>
`,
className: '',
iconSize: [45, 20]
});
}
}

var paris = [48.86, 2.35];
var parisLeft = [48.86, 2.25];
var parisRight = [48.86, 2.45];
var map = L.map('map', {
maxZoom: 18
}).setView(paris, 11);

var mcg = L.markerClusterGroup({
iconCreateFunction: customClusterIcon
}).addTo(map);
var category1 = L.layerGroup();
var category2 = L.layerGroup();

var cat2style = {
color: 'red',
category: 'cat2'
};

var markerA = L.circleMarker(paris).addTo(category1);
var markerB = L.circleMarker(paris).addTo(category1);
var markerC = L.circleMarker(paris, cat2style).addTo(category2);
var markerD = L.circleMarker(paris, cat2style).addTo(category2);

var markerE = L.circleMarker(parisLeft).addTo(category1);
var markerF = L.circleMarker(parisLeft).addTo(category1);

var markerG = L.circleMarker(parisRight, cat2style).addTo(category2);
var markerH = L.circleMarker(parisRight, cat2style).addTo(category2);

mcg.addLayers([category1, category2]);


L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
html,
body,
#map {
height: 100%;
margin: 0;
}

.cat1cluster {
background-color: #3388ff;
}

.cat2cluster {
background-color: red;
}

.cluster {
width: 20px;
height: 20px;
display: inline-block;
text-align: center;
}
<!-- Leaflet assets -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet-src.js" integrity="sha512-+ZaXMZ7sjFMiCigvm8WjllFy6g3aou3+GZngAtugLzrmPFKFK7yjSri0XnElvCTu/PrifAYQuxZTybAEkA8VOA==" crossorigin=""></script>

<!-- Leaflet.markercluster assets -->
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.css">
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.Default.css">
<script src="https://unpkg.com/leaflet.markercluster@1.4.1/dist/leaflet.markercluster-src.js"></script>


<div id="map"></div>

然后,如果您愿意,可以进一步自定义 spiderfication,以便它仅显示来自单击的类别群集图标的标记,以及类似的悬停多边形。

关于javascript - 防止多个 markerClusterGroup 图标在 Leaflet 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429440/

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