Need help with OverlappingMarkerSpiderfier-Leaflet?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

226 Stars 63 Forks 12 Commits 32 Opened issues


Deals with overlapping markers in the Leaflet maps API, Google Earth-style

Services available


Need anything else?

Contributors list

# 29,960
7 commits
# 476,307
1 commit
# 926
1 commit

h1. Overlapping Marker Spiderfier for Leaflet

Ever noticed how, in "Google Earth":, marker pins that overlap each other spring apart gracefully when you click them, so you can pick the one you meant?

And ever noticed how, when using the "Leaflet API":, the same thing doesn't happen?

This code makes Leaflet map markers behave in that Google Earth way (minus the animation). Small numbers of markers (yes, up to 8) spiderfy into a circle. Larger numbers fan out into a more space-efficient spiral.

The compiled code has no dependencies beyond Leaflet. And it's under 3K when compiled out of "CoffeeScript":, minified with Google's "Closure Compiler": and gzipped.

It's a port of my "original library for the Google Maps API": (Since the Leaflet API doesn't let us observe all the event types that the Google one does, the main difference between the original and the port is this: you must first call @[email protected] if and when you want to move a marker in the Leaflet version).

h3. Doesn't clustering solve this problem?

You may have seen the marker clustering libraries, which also help deal with markers that are close together.

That might be what you want. However, it probably isn't what you want (or isn't the only thing you want) if you have markers that could be in the exact same location, or close enough to overlap even at the maximum zoom level. In that case, clustering won't help your users see and/or click on the marker they're looking for.

h2. Demo

See the "demo map": (the data is random: reload the map to reposition the markers).

h2. Download

Download "the compiled, minified JS source":

Or use it straight from "cdnjs": @@.

h2. How to use

See the "demo map source":, or follow along here for a slightly simpler usage with commentary.

Create your map like normal (using the beautiful "Stamen watercolour OSM map":

bc. var map = new L.Map('map_canvas', {center: new L.LatLng(51.505, -0.09), zoom: 13}); var layer = new L.StamenTileLayer('watercolor'); map.addLayer(layer);

Create an @[email protected] instance:

bc. var oms = new OverlappingMarkerSpiderfier(map);

Instead of adding click listeners to your markers directly via @[email protected] or @[email protected], add a global listener on the @[email protected] instance instead. The listener will be passed the clicked marker as its first argument.

bc. var popup = new L.Popup(); oms.addListener('click', function(marker) { popup.setContent(marker.desc); popup.setLatLng(marker.getLatLng()); map.openPopup(popup); });

You can also add listeners on the @[email protected] and @[email protected] events, which will be passed an array of the markers affected. In this example, we observe only the @[email protected] event, using it to close any open @[email protected]:

bc. oms.addListener('spiderfy', function(markers) { map.closePopup(); });

Finally, tell the @[email protected] instance about each marker as you add it, using the @[email protected] method:

bc. for (var i = 0; i < window.mapData.length; i ++) { var datum = window.mapData[i]; var loc = new L.LatLng(, datum.lon); var marker = new L.Marker(loc); marker.desc = datum.d; map.addLayer(marker); oms.addMarker(marker); // <-- here }

h2. Docs

h3. Loading

The Leaflet @[email protected] object must be available when this code runs -- i.e. put the Leaflet API <script> tag before this one. The code has been tested with the 0.4 API version.

h3. Construction

bc. new OverlappingMarkerSpiderfier(map, options)

Creates an instance associated with @[email protected] (an @[email protected]).

The @[email protected] argument is an optional @[email protected] specifying any options you want changed from their defaults. The available options are:

keepSpiderfied (default: @[email protected])

By default, the OverlappingMarkerSpiderfier works like Google Earth, in that when you click a spiderfied marker, the markers unspiderfy before any other action takes place.

Since this can make it tricky for the user to work through a set of markers one by one, you can override this behaviour by setting the @[email protected] option to @[email protected]

nearbyDistance (default: @[email protected]).

This is the pixel radius within which a marker is considered to be overlapping a clicked marker.

circleSpiralSwitchover (default: @[email protected])

This is the lowest number of markers that will be fanned out into a spiral instead of a circle. Set this to @[email protected] to always get spirals, or @[email protected] for all circles.

legWeight (default: @[email protected])

This determines the thickness of the lines joining spiderfied markers to their original locations.

h3. Instance methods: managing markers

Note: methods that have no obvious return value return the OverlappingMarkerSpiderfier instance they were called on, in case you want to chain method calls.

bc. addMarker(marker)

Adds @[email protected] (an @[email protected]) to be tracked.

bc. removeMarker(marker)

Removes @[email protected] from those being tracked.

bc. clearMarkers()

Removes every @[email protected] from being tracked. Much quicker than calling @[email protected] in a loop, since that has to search the markers array every time.

bc. getMarkers()

Returns an @[email protected] of all the markers that are currently being tracked. This is a copy of the one used internally, so you can do what you like with it.

h3. Instance methods: managing listeners

bc. addListener(event, listenerFunc)

Adds a listener to react to one of three events.

@[email protected] may be @'click'@, @'spiderfy'@ or @'unspiderfy'@.

For @'click'@ events, @[email protected] receives one argument: the clicked marker object. You'll probably want to use this listener to do something like show an @[email protected]

For @'spiderfy'@ or @'unspiderfy'@ events, @[email protected] receives two arguments: first, an @[email protected] of the markers that were spiderfied or unspiderfied; second, an @[email protected] of the markers that were not. One use for these listeners is to make some distinction between spiderfied and non-spiderfied markers when some markers are spiderfied -- e.g. highlighting those that are spiderfied, or dimming out those that aren't.

bc. removeListener(event, listenerFunc)

Removes the specified listener on the specified event.

bc. clearListeners(event)

Removes all listeners on the specified event.

bc. unspiderfy()

Returns any spiderfied markers to their original positions, and triggers any listeners you may have set for this event. Unless no markers are spiderfied, in which case it does nothing. Be sure to call this before you call @[email protected] on any tracked marker.

h3. Properties

You can set the following properties on an OverlappingMarkerSpiderfier instance:

legColors.usual and legColors.highlighted

These determine the usual and highlighted colours of the lines.

You can also get and set any of the options noted in the constructor function documentation above as properties on an OverlappingMarkerSpiderfier instance.

h2. Licence

This software is released under the "MIT licence":

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.