Simple Leaflet MarkerCluster example with random GeoJSON data.

by Alex Azuero

HTML

<script src="http://cdn.leafletjs.com/leaflet-0.7.5/leaflet.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.5/leaflet.css">
<script src="https://rawgit.com/Leaflet/Leaflet.markercluster/leaflet-0.7/dist/leaflet.markercluster.js"></script>
<link rel="stylesheet" href="https://rawgit.com/Leaflet/Leaflet.markercluster/leaflet-0.7/dist/MarkerCluster.css">
<link rel="stylesheet" href="https://rawgit.com/Leaflet/Leaflet.markercluster/leaflet-0.7/dist/MarkerCluster.Default.css">
<div id="map"></div>

CSS

html, body, #map {
    height: 100%;
    width:100%;
    padding:0px;
    margin:0px;
    background: white;
}
.info {
    padding: 6px 8px;
    font: 14px/16px Arial, Helvetica, sans-serif;
    background: white;
    background: rgba(255, 255, 255, 0.8);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
    border-radius: 5px;
}
.info h4 {
    margin: 0 0 5px;
    color: #777;
}
.legend {
    text-align: left;
    line-height: 18px;
    color: #555;
}
.legend i {
    width: 18px;
    height: 18px;
    float: left;
    margin-right: 8px;
    opacity: 0.7;
}
.legend .colorcircle {
    border-radius: 50%;
    width: 15px;
    height: 15px;
    margin-top: 0px;
}
.legend .circlepadding {
    border-radius: 50%;
    margin-top: 0px;
    background: rgba(255, 255, 255, 0.8);
}

JavaScript

/////////////////////////////////////////////////////////////////////////////////////////////
//setting up the map// 
/////////////////////////////////////////////////////////////////////////////////////////////

// set center coordinates
var centerlat = 34.05;
var centerlon = -118.25;

// set default zoom level
var zoomLevel = 10;

// initialize map
var map = L.map('map').setView([centerlat, centerlon], zoomLevel);

// set source for map tiles
ATTR = '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'&copy; <a href="http://cartodb.com/attributions">CartoDB</a>';

CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';

// add tiles to map
L.tileLayer(CDB_URL, {attribution: ATTR}).addTo(map);

/////////////////////////////////////////////////////////////////////////////////////////////
//displaying the data//
/////////////////////////////////////////////////////////////////////////////////////////////

//create empty MarkerCluster group
var markers = L.markerClusterGroup();

//create an empty GeoJSON layer
var dotLayer = L.geoJson(false, {
    onEachFeature: onEachDot
});

//add dots every [dotInterval] ms until there are [dotCount] of them
var dotIndex = 1;
var dotCount = 500;

//populate GeoJSON layer
for (i=0 ; i< dotCount ; ++i) {
    var newDot = make_dot(i);
    dotLayer.addData(newDot);
}

//add GeoJSON layer to cluster layer and display it on the map
markers.addLayer(dotLayer);
map.addLayer(markers);  

/////////////////////////////////////////////////////////////////////////////////////////////
//styling functions//
/////////////////////////////////////////////////////////////////////////////////////////////

function onEachDot(feature, layer) {
    layer.bindPopup('<table style="width:150px"><tbody><tr><td><b>Name:</b></td><td>' + feature.properties.popup + '</td></tr><tr><td><b>Year:</b></td><td>' + feature.properties.year +...