MCPcopy
hub / github.com/Leaflet/Leaflet.markercluster

github.com/Leaflet/Leaflet.markercluster @v1.5.3 sqlite

repository ↗ · DeepWiki ↗ · release v1.5.3 ↗
105 symbols 246 edges 52 files 5 documented · 5%
README

Leaflet.markercluster

Provides Beautiful Animated Marker Clustering functionality for Leaflet, a JS library for interactive maps.

Requires Leaflet 1.0.0

cluster map example

For a Leaflet 0.7 compatible version, use the leaflet-0.7 branch

For a Leaflet 0.5 compatible version, Download b128e950

For a Leaflet 0.4 compatible version, Download the 0.2 release

Table of Contents

Using the plugin

Include the plugin CSS and JS files on your page after Leaflet files, using your method of choice: * Download the v1.4.1 release * Use unpkg CDN: https://unpkg.com/leaflet.markercluster@1.4.1/dist/ * Install with npm: npm install leaflet.markercluster

In each case, use files in the dist folder: * MarkerCluster.css * MarkerCluster.Default.css (not needed if you use your own iconCreateFunction instead of the default one) * leaflet.markercluster.js (or leaflet.markercluster-src.js for the non-minified version)

Building, testing and linting scripts

Install jake npm install -g jake then run npm install * To check the code for errors and build Leaflet from source, run jake. * To run the tests, run jake test.

Examples

See the included examples for usage.

The realworld example is a good place to start, it uses all of the defaults of the clusterer. Or check out the custom example for how to customise the behaviour and appearance of the clusterer

Usage

Create a new MarkerClusterGroup, add your markers to it, then add it to the map

var markers = L.markerClusterGroup();
markers.addLayer(L.marker(getRandomLatLng(map)));
... Add more layers ...
map.addLayer(markers);

Options

Defaults

By default the Clusterer enables some nice defaults for you: * showCoverageOnHover: When you mouse over a cluster it shows the bounds of its markers. * zoomToBoundsOnClick: When you click a cluster we zoom to its bounds. * spiderfyOnMaxZoom: When you click a cluster at the bottom zoom level we spiderfy it so you can see all of its markers. (Note: the spiderfy occurs at the current zoom level if all items within the cluster are still clustered at the maximum zoom level or at zoom specified by disableClusteringAtZoom option) * removeOutsideVisibleBounds: Clusters and markers too far from the viewport are removed from the map for performance. * spiderLegPolylineOptions: Allows you to specify PolylineOptions to style spider legs. By default, they are { weight: 1.5, color: '#222', opacity: 0.5 }.

You can disable any of these as you want in the options when you create the MarkerClusterGroup:

var markers = L.markerClusterGroup({
    spiderfyOnMaxZoom: false,
    showCoverageOnHover: false,
    zoomToBoundsOnClick: false
});

Customising the Clustered Markers

As an option to MarkerClusterGroup you can provide your own function for creating the Icon for the clustered markers. The default implementation changes color at bounds of 10 and 100, but more advanced uses may require customising this. You do not need to include the .Default css if you go this way. You are passed a MarkerCluster object, you'll probably want to use getChildCount() or getAllChildMarkers() to work out the icon to show.

var markers = L.markerClusterGroup({
    iconCreateFunction: function(cluster) {
        return L.divIcon({ html: '<b>' + cluster.getChildCount() + '</b>' });
    }
});

Check out the custom example for an example of this.

If you need to update the clusters icon (e.g. they are based on markers real-time data), use the method refreshClusters().

Customising Spiderfy shape positions

You can also provide a custom function as an option to MarkerClusterGroup to override the spiderfy shape positions. The example below implements linear spiderfy positions which overrides the default circular shape.

var markers = L.markerClusterGroup({
    spiderfyShapePositions: function(count, centerPt) {
                var distanceFromCenter = 35,
                    markerDistance = 45,
                    lineLength = markerDistance * (count - 1),
                    lineStart = centerPt.y - lineLength / 2,
                    res = [],
                    i;

                res.length = count;

                for (i = count - 1; i >= 0; i--) {
                    res[i] = new Point(centerPt.x + distanceFromCenter, lineStart + markerDistance * i);
                }

                return res;
            }
});

All Options

Enabled by default (boolean options)

  • showCoverageOnHover: When you mouse over a cluster it shows the bounds of its markers.
  • zoomToBoundsOnClick: When you click a cluster we zoom to its bounds.
  • spiderfyOnMaxZoom: When you click a cluster at the bottom zoom level we spiderfy it so you can see all of its markers. (Note: the spiderfy occurs at the current zoom level if all items within the cluster are still clustered at the maximum zoom level or at zoom specified by disableClusteringAtZoom option).
  • removeOutsideVisibleBounds: Clusters and markers too far from the viewport are removed from the map for performance.
  • animate: Smoothly split / merge cluster children when zooming and spiderfying. If L.DomUtil.TRANSITION is false, this option has no effect (no animation is possible).

Other options

  • animateAddingMarkers: If set to true (and animate option is also true) then adding individual markers to the MarkerClusterGroup after it has been added to the map will add the marker and animate it into the cluster. Defaults to false as this gives better performance when bulk adding markers. addLayers does not support this, only addLayer with individual Markers.
  • disableClusteringAtZoom: If set, at this zoom level and below, markers will not be clustered. This defaults to disabled. See Example. Note: you may be interested in disabling spiderfyOnMaxZoom option when using disableClusteringAtZoom.
  • maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80. Decreasing will make more, smaller clusters. You can also use a function that accepts the current map zoom and returns the maximum cluster radius in pixels.
  • polygonOptions: Options to pass when creating the L.Polygon(points, options) to show the bounds of a cluster. Defaults to empty, which lets Leaflet use the default Path options.
  • singleMarkerMode: If set to true, overrides the icon for all added markers to make them appear as a 1 size cluster. Note: the markers are not replaced by cluster objects, only their icon is replaced. Hence they still react to normal events, and option disableClusteringAtZoom does not restore their previous icon (see #391).
  • spiderLegPolylineOptions: Allows you to specify PolylineOptions to style spider legs. By default, they are { weight: 1.5, color: '#222', opacity: 0.5 }.
  • spiderfyDistanceMultiplier: Increase from 1 to increase the distance away from the center that spiderfied markers are placed. Use if you are using big marker icons (Default: 1).
  • iconCreateFunction: Function used to create the cluster icon. See the default implementation or the custom example.
  • spiderfyShapePositions: Function used to override spiderfy default shape positions.
  • clusterPane: Map pane where the cluster icons will be added. Defaults to L.Marker's default (currently 'markerPane'). See the pane example.

Chunked addLayers options

Options for the addLayers method. See #357 for explanation on how the chunking works. * chunkedLoading: Boolean to split the addLayers processing in to small intervals so that the page does not freeze. * chunkInterval: Time interval (in ms) during which addLayers works before pausing to let the rest of the page process. In particular, this prevents the page from freezing while adding a lot of markers. Defaults to 200ms. * chunkDelay: Time delay (in ms) between consecutive periods of processing for addLayers. Default to 50ms. * chunkProgress: Callback function that is called at the end of each chunkInterval. Typically used to implement a progress indicator, e.g. code in RealWorld 50k. Defaults to null. Arguments: 1. Number of processed markers 2. Total number of markers being added 3. Elapsed time (in ms)

Events

Leaflet events like click, mouseover, etc. are just related to Markers in the cluster. To receive events for clusters, listen to 'cluster' + '<eventName>', ex: clusterclick, clustermouseover, clustermouseout.

Set your callback up as follows to handle both cases:

markers.on('click', function (a) {
    console.log('marker ' + a.layer);
});

markers.on('clusterclick', function (a) {
    // a.layer is actually a cluster
    console.log('cluster ' + a.layer.getAllChildMarkers().length);
});

Additional MarkerClusterGroup Events

  • animationend: Fires when marker clustering/unclustering animation has completed
  • spiderfied: Fires when overlapping markers get spiderified (Contains cluster and markers attributes)
  • unspiderfied: Fires when overlapping markers get unspiderified (Contains cluster and markers attributes)

Methods

Group methods

Adding and removing Markers

addLayer, removeLayer and clearLayers are supported and they should work for most uses.

Bulk adding and removing Markers

addLayers and removeLayers are bulk methods for adding and removing markers and should be favoured over the single versions when doing bulk addition/removal of markers. Each takes an array of markers. You can use dedicated options to fine-tune the behaviour of addLayers.

These methods extract non-group layer children from Layer Group types, even deeply nested. However, be noted that: - chunkProgress jumps backward when addLayers finds a group (since appending its children to the input array makes the total increase). - Groups are not actually added into the MarkerClusterGroup, only their non-group child layers. Therfore, hasLayer method will return true for non-group child layers, but false on any (possibly parent) Layer Group types.

If you are removing a lot of markers it will almost definitely be better to call clearLayers then call addLayers to add the markers you don't want to remove back in. See #59 for details.

Getting the visible parent of a marker

If you have a marker in your MarkerClusterGroup and you want to get the visible parent of it (Either itself or a cluster it is contained in that is currently visible on the map). This will return null if the marker and its parent clusters are not visible currently (they are not

Core symbols most depended-on inside this repo

expect
called by 516
spec/expect.js
i
called by 55
spec/expect.js
delegateToCalls
called by 22
spec/sinon.js
mirrorPropAsAssertion
called by 19
spec/sinon.js
stylize
called by 16
spec/expect.js
getClusterAtZoom
called by 13
spec/suites/RefreshSpec.js
match
called by 8
spec/sinon.js
each
called by 8
spec/sinon.js

Shape

Function 105

Languages

TypeScript100%

Modules by API surface

spec/sinon.js69 symbols
spec/expect.js24 symbols
spec/suites/removeOutsideVisibleBoundsSpec.js4 symbols
spec/suites/RefreshSpec.js4 symbols
src/MarkerClusterGroup.js1 symbols
spec/suites/zoomAnimationSpec.js1 symbols
spec/suites/SpecHelper.js1 symbols
Jakefile.js1 symbols

Dependencies from manifests, versioned

git-rev-sync1.8.0 · 1×
happen0.3.1 · 1×
jake8.0.19 · 1×
jshint2.9.4 · 1×
karma4.0.0 · 1×
karma-chrome-launcher2.0.0 · 1×
karma-coverage1.1.1 · 1×
karma-firefox-launcher1.0.1 · 1×
karma-mocha1.3.0 · 1×
karma-phantomjs-launcher1.0.4 · 1×
karma-rollup-preprocessor7.0.0 · 1×
karma-safari-launcher1.0.0 · 1×

For agents

$ claude mcp add Leaflet.markercluster \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact