如何使用传单的 map.eachLayer 找到标记?

Kir*_*til 2 marker 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
   }
Run Code Online (Sandbox Code Playgroud)

谢谢。

JRI*_*JRI 5

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

这对我有用:

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

  • 很高兴它有所帮助。事后看来,`if (layer instanceof L.Marker)` 可能是一个更好的测试。 (3认同)