Replacing default Leaflet L.control.layers with a set of buttons
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="http://leaflet.github.io/Leaflet.markercluster/dist/leaflet.markercluster-src.js"></script>
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.markercluster/dist/MarkerCluster.css">
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.markercluster/dist/MarkerCluster.Default.css">
<script src="http://code.jquery.com/jquery-2.2.2.min.js"></script>
<script src="https://leaflet.github.io/Leaflet.markercluster/example/realworld.388.js"></script>
<div>
<select id="mySelector"></select>
</div>
<div id="map"></div>
CSS
html,
body,
#map {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}
#mySelector {
position: absolute;
z-index: 1;
top: 20px;
left: 20px;
}
.myButtonBar {
background: white;
box-shadow: 0 1px 7px rgba(0, 0, 0, 0.25);
-webkit-border-radius: 4px;
border-radius: 4px;
text-align: center;
padding: 0px 10px;
position: relative;
}
.leaflet-verticalcenter {
position: absolute;
top: 50%;
transform: translateY(-50%);
padding-top: 10px;
}
.leaflet-verticalcenter .leaflet-control {
margin-bottom: 10px;
}
.leaflet-horizontalcenter {
position: absolute;
left: 50%;
transform: translateX(-50%);
padding-top: 10px;
}
.leaflet-horizontalcenter .leaflet-control {
margin-bottom: 10px;
}
.switch-field {
font-family: "Lucida Grande", Tahoma, Verdana, sans-serif;
padding: 5px;
overflow: hidden;
}
.switch-title {
font-size: 16px;
}
.switch-title:hover {
cursor: default;
}
.switch-field input {
display: none;
}
.switch-field label {
float: left;
}
.switch-field label {
display: inline-block;
width: 65px;
background-color: #f4f4f4;
color: rgba(0, 0, 0, 0.65);
font-size: 14px;
font-weight: normal;
text-align: center;
text-shadow: none;
padding: 2px 12px;
border: 1px solid rgba(0, 0, 0, 0.2);
-webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
-webkit-transition: all 0.1s ease-in-out;
-moz-transition: all 0.1s ease-in-out;
-ms-transition: all 0.1s ease-in-out;
-o-transition: all 0.1s ease-in-out;
transition: all 0.1s ease-in-out;
}
.switch-field label:hover {
cursor: pointer;
}
.switch-field input:checked + label {
background-color: #444;
-webkit-box-shadow: none;
box-shadow: none;
color: rgba(255, 255, 255, 1);
}
.switch-field label:first-of-type {
border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
...
JavaScript
// Create map
var map = L.map("map", {
center: [53.905, 27.562],
zoom: 11,
maxZoom: 18,
zoomControl: false
});
// Store for all markers
var allMarkers = L.layerGroup();
// Handle map layeradd event
map.on("layeradd", function(e) {
if (!e.layer.options) {
return;
}
// Handle only marker layers
if ((e.layer.options.id != "markerLayer1") && (e.layer.options.id != "markerLayer2") && (e.layer.options.id != "markerLayer3")) {
return;
}
// For the currently added layer (which is one with markers)
// get all its layers
var markers = e.layer.getLayers();
console.clear();
// Get the dropdown
var mySelector = $("#mySelector");
// Sort list items according their ID numbers
markers.sort(function(a, b) {
var aId = parseInt(a.feature.properties.id, 10),
bId = parseInt(b.feature.properties.id, 10);
return aId < bId ? -1 : aId > bId ? 1 : 0
}).forEach(function(marker) {
mySelector.append("<option value='" + L.stamp(marker) + "'>" + marker.feature.properties.id // id added
+ '. ' + marker.feature.properties.name + "</option>");
})
});
// Handle map layeradd event
map.on("layerremove", function(e) {
if (!e.layer.options) {
return;
}
// Handle only marker layers
if ((e.layer.options.id != "markerLayer1") && (e.layer.options.id != "markerLayer2") && (e.layer.options.id != "markerLayer3")) {
return;
}
// For the currently removed layer (which is one with markers)
// get all its layers
var markers = e.layer.getLayers();
// Get the dropdown
var mySelector = $("#mySelector");
// Remove options from the dropdown menu
for (var i = 0; i < markers.length; i++) {
mySelector.find("option[value='" + L.stamp(markers[i]) + "']").remove();
}
});
// Zoom on select change
$("#mySelector").change(function(e) {
var selected = allMarkers.getLayer($(this).val());
if (markerLayer1.hasLayer(selected)) {
markerLayer1.zoomToShowLayer(selected, function() {
...