如何使用 map.eachLayer of leaflet 查找标记?

How to find a marker using map.eachLayer of leaflet?

我可以显示集群标记的内容,但无法显示单个标记的内容。

请找到下面的代码,其中 else 语句将处理单个标记。

map.eachLayer(function(marker) {  
   if (marker.getChildCount) { // to get cluster marker details
     console.log('cluster length ' + marker.getAllChildMarkers().length);
   } else {
     // how to display the contents of a single marker ??

     alert(marker.options.title); // doesn't work
   }

谢谢。

请记住,map.eachLayer() 适用于地图的 每个 图层。这包括弹出窗口和地图图块层,以及标记和标记簇。并非所有这些类型的图层都具有与标记或群集相同的选项。您将需要测试以检查每一层是否是标记、集群或其他东西。另外,请记住,标记不一定要设置标题。

这对我有用:

map.eachLayer(function(layer) {
    if(layer.options && layer.options.pane === "markerPane") {
        alert("Marker [" + layer.options.title + "]");
    }
});