JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-beta.2.rc.2/leaflet.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<button id='addButton' onclick='simulation.addImages()'>Add Images</button>
<button id='removeButton' onclick='simulation.removeImaegs()'>Remove Images</button>
<div id="map"></div>
CSS
#map {
height: 450px;
}
JavaScript
class Simulation
{
constructor()
{
// center of the map
var center = [1.8650, 51.2094];
// Create the map
var map = L.map('map').setView(center, 2);
// Set up the OSM layer
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
this.imagesLayer = new L.layerGroup();
this.imagesLayer.addTo(map);
}
addImages()
{
console.log('ok, add')
for (var i=0; i<3000; i++)
{
var flagIcon = L.icon({
iconUrl: 'https://cdn-icons-png.flaticon.com/512/196/196817.png',
iconSize: [15, 15], // size of the icon
shadowSize: [18, 18], // size of the shadow
iconAnchor: [18, 18], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-1, -1] // point from which the popup should open relative to the iconAnchor
});
var randLat = Math.floor(Math.random() * 90);
randLat *= Math.round(Math.random()) ? 1 : -1;
var randLon = Math.floor(Math.random() * 180);
randLon *= Math.round(Math.random()) ? 1 : -1;
var flag = L.marker([randLat,randLon], {icon: flagIcon})
this.imagesLayer.addLayer(flag)
}
}
removeImaegs()
{
console.log('ok removed')
this.imagesLayer.clearLayers();
}
}
var simulation = new Simulation();