I want to render about 10.000 markers or dots on a leaflet map. I already did it the regular way and I found it is way slower compared to Google Maps. I'm looking for a way to render multiple elements without getting the performance issues.
Is there a way to do this with Leaflet?
Update: I don't want to plot with bright dots that can't handle events. I want to actually paint markers with different colors and events.
The Clusterer can handle 10,000 or even 50,000 markers (in chrome).
Adding a Simple Marker Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.
You just need to filter the clicked marker' coords not to be included anymore to your state variable and that's it!
There are a number of reasons why your map might not be displaying: You have an error in your JavaScript (most likely) - use whichever debugging tool is provided with your browser to verify. you are using Internet Explorer and you have Compatibility mode ON....
The performance issue is due to the fact that each marker is an individual DOM element. Browsers struggle in rendering thousands of them.
In your case, an easy workaround would be instead to use Circle Markers and have them rendered on a Canvas (e.g. using map preferCanvas
option, or with a specific canvas renderer that you pass as renderer
option for each of your Circle Marker). That is how Google Maps works by default: its markers are actually drawn on a Canvas.
var map = L.map('map', { preferCanvas: true }); var circleMarker = L.circleMarker(latLng, { color: '#3388ff' }).addTo(map);
or
var map = L.map('map'); var myRenderer = L.canvas({ padding: 0.5 }); var circleMarker = L.circleMarker(latLng, { renderer: myRenderer, color: '#3388ff' }).addTo(map);
With this solution, each Circle Marker is no longer an individual DOM element, but instead is drawn by Leaflet onto a single Canvas, which is much easier to handle for the browser.
Furthermore, Leaflet still tracks the mouse position and related events and triggers the corresponding events on your Circle Markers, so that you can still listen to such events (like mouse click, etc.).
Demo with 100k points: https://jsfiddle.net/sgu5dc0k/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With