-
-
Save ThomasG77/f99ef32c5950f246275488c1736dc05c to your computer and use it in GitHub Desktop.
Sort layers in Leaflet https://stackoverflow.com/questions/51194461/sort-order-of-layers-in-leaflet-layer-control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Layers Control Tutorial - Leaflet</title> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" /> | |
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin=""/> | |
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js" integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw==" crossorigin=""></script> | |
<style> | |
html, body { | |
height: 100%; | |
margin: 0; | |
} | |
#map { | |
width: 600px; | |
height: 400px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
// 4 in first group | |
var cities = L.layerGroup(undefined, {optionTosort: 9}); | |
L.marker([39.61, -105.02]).bindPopup('This is Littleton, CO.').addTo(cities), | |
L.marker([39.74, -104.99]).bindPopup('This is Denver, CO.').addTo(cities), | |
L.marker([39.73, -104.8]).bindPopup('This is Aurora, CO.').addTo(cities), | |
L.marker([39.77, -105.23]).bindPopup('This is Golden, CO.').addTo(cities); | |
// 1 in second group | |
var otherlayer1 = L.layerGroup(undefined, {optionTosort: 3}); | |
L.marker([39.7253, -104.96]).bindPopup('This is somewhere').addTo(otherlayer1); | |
// 1 in third group | |
var otherlayer2 = L.layerGroup(undefined, {optionTosort: 4}); | |
L.marker([39.763, -104.86]).bindPopup('This is somewhere else').addTo(otherlayer2); | |
// add the OpenStreetMap tiles | |
var grayscale = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', { | |
maxZoom: 18, | |
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' | |
}); | |
var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | |
maxZoom: 19, | |
attribution: '© <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>' | |
}); | |
var map = L.map('map', { | |
center: [39.73, -104.99], | |
zoom: 10, | |
layers: [grayscale, cities, otherlayer1, otherlayer2] | |
}); | |
var baseLayers = { | |
"Grayscale": grayscale, | |
"Streets": streets | |
}; | |
var overlays = { | |
"Cities": cities, | |
"otherlayer1": otherlayer1, | |
"otherlayer2": otherlayer2 | |
}; | |
// Config borrowed mainly from https://github.com/Leaflet/Leaflet/blob/922593addb7ba4a7f5f8a2793d53e03cd04c0b9c/spec/suites/control/Control.LayersSpec.js#L342 | |
L.control.layers(baseLayers, overlays, { | |
sortLayers: true, | |
sortFunction: function (a, b) { | |
return a.options.optionTosort - b.options.optionTosort; | |
} | |
}).addTo(map); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment