Over a million developers have joined DZone.

Awesome Clustered Markers in Leaflet

DZone's Guide to

Awesome Clustered Markers in Leaflet

Learn about these super cool clustered markers in leaflet.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Some time ago someone asked at gis.stackexchange.com for nice cluster marker implementation, both for Leaflet or OpenLayers, something similar to Redfin. A couple of weeks ago I discover a great implementation for Leaflet, the Leaflet.markercluster from Dave Leaver. Simply awesome !!!


The Leaflet.markercluster plugin for Leaflet is documented enough on its web page, so I’m not going to repeat it here, but I would like to make a short summary of its features.

Add/Remove Marker

 We can easily add or remove marker with the methods addLayer and removeLayer. For example:

markers.addLayer( new L.Marker(new L.LatLng(lat, lon), { title: title }) );



We have available the events clusterclick, clustermouseover, clustermouseout and zoomend to interact with the new layer.


The Leaflet.markercluster plugin has some nice options, which by default they are set as:

options: {
 maxClusterRadius: 60, 
 iconCreateFunction: null,
 spiderfyOnMaxZoom: true,
 showCoverageOnHover: true,
 zoomToBoundsOnClick: true
  • maxClusterRadius: All the markers within this radius becomes a cluster.
  • iconCreateFunction: Allows to customize the icons for the clusters. (See more here).
  • spiderfyOnMaxZoom: When you click a cluster at the bottom zoom level we spiderfy it so you can see all of its markers.
  • showCoverageOnHover: When you click a cluster we zoom to its bounds.
  • zoomToBoundsOnClick: When you mouse over a cluster it shows the bounds of its markers.
To change the default values simply specify the values when instantiation the cluster group:
var markers = new L.MarkerClusterGroup({ 
 maxClusterRadius: 100, 
 spiderfyOnMaxZoom: true, 
 showCoverageOnHover: false, 
 zoomToBoundsOnClick: true 

A Short Note

Many posts ago I write about Open Alternatives to Google Maps where I talk about the nice Leaflet project, finishing with the sentence: Believe me, put an eye on this project, it has many more to say. And seems this great marker cluster is one of that thins to say :) In addition, I was seeing lot of activity about the Leaflet project, much more than on OpenLayers.

Do not confuse please, I’m talking about activity related to news where many web pages are changing from Google Maps API to Leaflet. I know the OpenLayers community is make a great job, as always, on the next major release of the project, OpenLayers 3.0, with a great amount of improvements.

At this moment, the comparison between Leaflet and OpenLayers is something like comparing the clothes to go to run or an armor to go to a battle.  What I want to say is OpenLayers is a big and complex project but, because of this, it allows to do almost anything you can need in a GIS application. On the other hand Leaflet is a more agile and lightweight project, designed to to only a bunch of things. While OpenLayers allows to access tile server (TMS or WMTS), WMS, WFS, etc Leaflet is designed to access TMS (like OpenStreetMap, CloudMade tiles, etc).

The summary is Leaflet implements the 10% (to say a number) of things implements OpenLayers but that 10% is the required to do the most common things for a weg application.


Who wants to create an awesome cluster implementation for OpenLayers?

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

web dev ,leaflet

Published at DZone with permission of Antonio Santiago, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}