Mapbox / Leaflet.Markercluster getLayers()
by Jason
HTML
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css">
<script src="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.js"></script>
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.markercluster/dist/MarkerCluster.css">
<script src="http://leaflet.github.io/Leaflet.markercluster/dist/leaflet.markercluster-src.js"></script>
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.markercluster/dist/MarkerCluster.Default.css">
<div id="map"></div>
CSS
/* Map */
#map {
height: 440px;
}
JavaScript
map = L.map('map', {
center: [7.2, 40.9],
zoom: 2
});
L.tileLayer('http://{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.png', {
attribution: "Map: Tiles Courtesy of MapQuest (OpenStreetMap, CC-BY-SA)",
subdomains: ["otile1", "otile2", "otile3", "otile4"],
maxZoom: 12,
minZoom: 2
}).addTo(map);
var markers = new L.MarkerClusterGroup();
markers.addLayer(new L.Marker([1, 1]));
markers.addTo(map);
// expect getLayers().length to be 1, which will add
// another marker, but it does not happen
if (markers.getLayers().length == 1) {
markers.addLayer(new L.Marker([20, 20]));
}