Simple Leaflet MarkerCluster example with random GeoJSON data.
by yccu
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
function getLocations(lat, long) {
$.ajax({
method: 'GET',
url: 'https://a-server.be/slash/getStores.php'
}).done(function(data) {
getLocation(data);
});
}
getLocations();
function getLocation(data) {
var records = JSON.parse(data);
var iconSrc = records.icon;
var lat = records.lat;
var long = records.long;
getLocation()
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
// set center coordinates
var lat1 = position.coords.latitude;
var lon1 = position.coords.longitude;
// set default zoom level
var zoomLevel = 10;
// initialize map
var map = L.map('map').setView([lat1, lon1], zoomLevel);
CDB_URL = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
// add tiles to map
L.tileLayer(CDB_URL).addTo(map);
//create empty MarkerCluster group
var markers = L.markerClusterGroup();
//populate GeoJSON layer
for (i = 0; i < records.length; i++) {
L.marker([records[i].lat, records[i].long], {
icon: icon1
}).addTo(map)
}
//add GeoJSON layer to cluster layer and display it on the map
markers.addLayer(dotLayer);
map.addLayer(markers);
}}