search

Home  >  Q&A  >  body text

Leaflet gets all polylines added to map

<p>Once you've added one or more polylines with clickable actions on your map, and want to use the measure tool <em>PolylineMeasure</em>, those polylines will exist in some way. How can I get all created polylines in a map to disable clickability, just like you can with polylines directly you can click with marker on the map. </p> <pre class="brush:php;toolbar:false;">L.polyline(latlngs, {clickable: false}).addTo(map);</pre>
P粉358281574P粉358281574474 days ago449

reply all(1)I'll reply

  • P粉138711794

    P粉1387117942023-08-19 09:54:58

    Polyline disable in Leaflet

    You need to traverse all layers, you can use the eachLayer function to achieve this. And remove the click event listener. Just use the .off function like this: layer.off('click'); In my code below it will remove the line from each polylineRemove event listener.

    The following is the code:

    // 我假设'map'是您的L.Map实例。
    map.eachLayer(function(layer) {
      if (layer instanceof L.Polyline) {
        // 从该折线中移除点击事件监听器。
        layer.off('click');
      }
    });

    reply
    0
  • Cancelreply