我面临传单聚类问题。我使用 v-marker-cluster。在集群的图标 createFunction 中,我使用子 className 来确定集群的样式。这是该函数的一部分:
const childCount = marker_cluster._markers.filter((marker) => marker.options.icon.options.className.includes("selected")).length;
// other things that do not affect childCount
return new L.DivIcon({
html: "<div>" + childCount + "</div>",
className: childCount == 0 ? "class1" : "class2"),
});
但是在这样做时,我发现有时我有 class1,即使集群有一些具有选定类别的子级。 可以肯定的是,我检查了对象marker_cluster,发现_childCount等于2并且_markers为空。
这怎么可能? _markers 不是子元素的代表吗? (大部分时间都有效,但不知道为什么,有时会是空的)
我发现你应该使用
childMarkers
方法访问 getAllChildMarkers()
以避免此类问题