Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of adding all markers directly on the map, you can add the markers on a separate layer (i.e. <code>var markers = new L.FeatureGroup();</code>) and then add that layer on the map (<code>map.addLayer(markers);</code>) outside the loop.</p> <p>For example,</p> <p><a href="http://jsfiddle.net/9BXL7/">http://jsfiddle.net/9BXL7/</a></p> <p><strong>html</strong></p> <pre><code>&lt;button&gt;remove all markers&lt;/button&gt; &lt;div id="map"&gt;&lt;/div&gt; </code></pre> <p><strong>css</strong></p> <pre><code>html, body, #map { width: 100%; height: 100%; padding: 0; margin: 0; } </code></pre> <p><strong>js</strong></p> <pre><code>var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/997/256/{z}/{x}/{y}.png', { maxZoom: 18, attribution: 'Map data &amp;copy; 2011 OpenStreetMap contributors, Imagery &amp;copy; 2011 CloudMade', key: 'BC9A493B41014CAABB98F0471D759707' }); var map = L.map('map') .setView([50.5, 30.51], 15) .addLayer(cloudmade); var markers = new L.FeatureGroup(); function getRandomLatLng(map) { var bounds = map.getBounds(), southWest = bounds.getSouthWest(), northEast = bounds.getNorthEast(), lngSpan = northEast.lng - southWest.lng, latSpan = northEast.lat - southWest.lat; return new L.LatLng( southWest.lat + latSpan * Math.random(), southWest.lng + lngSpan * Math.random()); } function populate() { for (var i = 0; i &lt; 10; i++) { var marker = L.marker(getRandomLatLng(map)); marker.bindPopup("&lt;p&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.&lt;/p&gt;&lt;p&gt;Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.&lt;/p&gt;", { showOnMouseOver: true }); markers.addLayer(marker); } return false; } map.addLayer(markers); populate(); function removeAllMarkers(){ map.removeLayer(markers); } document.querySelector('button').onclick=function(){removeAllMarkers()}; </code></pre> <p>Should you need to DELETE or clear the markers layer to swap out the points in the future use:</p> <pre><code>markers.clearLayers(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload