2012-07-10 18:54:39 -07:00
Leaflet.markercluster
=====================
2012-07-24 14:45:50 +12:00
Provides Beautiful Animated Marker Clustering functionality for Leaflet
2012-07-11 14:13:46 +12:00
2012-07-16 16:33:57 +12:00
## Using the plugin
2012-07-24 14:22:30 +12:00
See the included examples for usage.
The [everything example ](http://danzel.github.com/Leaflet.markercluster/example/marker-clustering-everything.html ) is a good place to start, it utilises the MarkerCluser.Default class to provide all of the default functionality.
Or check out the [custom example ](http://danzel.github.com/Leaflet.markercluster/example/marker-clustering-custom.html ) for how to customise the behaviour and appearance of the clusterer
2012-07-11 14:13:46 +12:00
2012-07-24 14:22:30 +12:00
### Usage
2012-07-11 14:13:46 +12:00
Create a new MarkerClusterGroup, add your markers to it, then add it to the map
2012-07-12 00:15:35 +03:00
```javascript
2012-07-11 14:13:46 +12:00
var markers = new L.MarkerClusterGroup();
markers.addLayer(new L.Marker(getRandomLatLng(map)));
2012-07-16 16:33:57 +12:00
... Add more layers ...
2012-07-11 14:13:46 +12:00
map.addLayer(markers);
2012-07-12 00:15:35 +03:00
```
2012-07-11 14:13:46 +12:00
2012-07-24 14:22:30 +12:00
### Defaults
As a safe default you can use the included MarkerCluster.Default.(css/js) to provide default behaviour and appearance of the clusters.
2012-07-11 14:13:46 +12:00
2012-07-24 14:22:30 +12:00
Include the .Default files (or use the prebuilt version) and create a MarkerClusterGroup as follows:
```javascript
var markers = new L.MarkerClusterGroup();
L.MarkerClusterDefault.bindEvents(map, markers);
... Add markers to the MarkerClusterGroup ...
map.addLayer(markers);
```
### Customising the Clustered Markers
2012-07-11 14:13:46 +12:00
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.
2012-07-24 14:22:30 +12:00
You do not need to include the .Default files if you go this way.
2012-07-11 14:13:46 +12:00
2012-07-12 00:15:35 +03:00
```javascript
2012-07-11 14:13:46 +12:00
var markers = new L.MarkerClusterGroup({ options: {
iconCreateFunction: function(childCount) {
return new L.DivIcon({ html: '<b>' + childCount + '</b>' });
}
}});
2012-07-16 16:33:57 +12:00
```
2012-07-24 14:22:30 +12:00
Check out the [custom example ](http://danzel.github.com/Leaflet.markercluster/example/marker-clustering-custom.html ) for an example of this.
2012-07-16 16:33:57 +12:00
### Events
2012-07-24 14:22:30 +12:00
If you register for click, mouseover, etc events just related to Markers in the cluster.
To recieve events for clusters listen to 'cluster' + 'eventIWant', ex: 'clusterclick', 'clustermouseover'.
2012-07-16 16:33:57 +12:00
Set your callback up as follows to handle both cases:
```javascript
markers.on('click', function (a) {
2012-07-24 14:22:30 +12:00
console.log('marker ' + a.layer);
});
markers.on('clusterclick', function (a) {
console.log('cluster ' + a.layer.getAllChildMarkers().length);
2012-07-16 16:33:57 +12:00
});
```
2012-07-18 11:55:21 +12:00
### Getting the bounds of a cluster
When you recieve an event from a cluster you can query it for the bounds.
See [example/marker-clustering-convexhull.html ](http://danzel.github.com/Leaflet.markercluster/example/marker-clustering-convexhull.html ) for a working example.
```javascript
2012-07-24 14:22:30 +12:00
markers.on('clusterclick', function (a) {
map.addLayer(new L.Polygon(a.layer.getConvexHull()));
2012-07-18 11:55:21 +12:00
});
```
2012-07-16 16:33:57 +12:00
2012-07-18 11:55:21 +12:00
### Zooming to the bounds of a cluster
When you recieve an event from a cluster you can zoom to its bounds in one easy step.
See [marker-clustering-zoomtobounds.html ](http://danzel.github.com/Leaflet.markercluster/example/marker-clustering-zoomtobounds.html ) for a working example.
```javascript
2012-07-24 14:22:30 +12:00
markers.on('clusterclick', function (a) {
a.layer.zoomToBounds();
2012-07-18 11:55:21 +12:00
});
```
### License
2012-07-16 16:33:57 +12:00
Leaflet.markercluster is free software, and may be redistributed under the MIT-LICENSE.