Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for handling thousands of markers with Leaflet

I'm doing some tests with an HTML map in conjunction with Leaflet. Server side I have a Ruby Sinatra app serving json markers fetched by a MySQL table. What are the best practices working with 2k-5k and potentially more markers?

  • Load all the markers in the first place and then delegate everything to Leaflet.markercluster.
  • Load the markers every time the map viewport change, sending southWest & northEast points to the server, elaborate the clipping server side and then sync the marker buffer client side with the server-fetched entries (what I'm doing right now).
  • A mix of the two above approaches.

Thanks, Luca

like image 958
Luca Anceschi Avatar asked Feb 15 '14 08:02

Luca Anceschi


People also ask

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).

How do you show markers in leaflet?

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.

How do you add a custom marker in leaflet?

Now you can above icon for the marker in the map as below: L. marker([51.5, -0.09], {icon: greenIcon}). addTo(map);


2 Answers

A few months have passed since I originally posted the question and I made it through!

As @Brett DeWoody correctly noted the right approach is to be strictly related to the number of DOM elements on the screen (I'm referring mainly to markers). The more the merrier if your device is faster (CPU especially). Since the app I was developing has both desktop and tablet as target devices, CPU was a relevant factor just like the marker density of different geo-areas.

I decided to separate DBase querying/fetching and map representation/displaying. Basically, the user adjusts controls/inputs to filter the whole dataset, afterward records are fetched and Leaflet.markercluster does the job of representation. When a filter is modified the cycle starts over. Users can choose the map zoom level of clusterization depending on their CPU power.

In my particular scenario, the above-mentioned represented the best approach (verified by console.time). I found that viewport optimization was good for lower marker-density areas (a pity).

Hope it may be helpful.

Cheers, Luca

like image 52
Luca Anceschi Avatar answered Oct 17 '22 16:10

Luca Anceschi


Try options and optimize when you see issues rather than optimizing early. You can probably get away with just Leaflet.markercluster unless your markers have lots of data attached to them.

like image 28
tmcw Avatar answered Oct 17 '22 15:10

tmcw