<html>
<head>
<meta name="description" content="How to identify overlapping JSON layers in Leaflet?">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Identify overlapping JSON layers</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.2/dist/leaflet.css" />
</head>
<body>
<p>Note that the Campus layer can be identified when the States layer is not present, but is over-lapped when the States layer is on</p>
<div id='map'></div>
<label><input id="chkStates" type="checkbox">Toggle States layer</label>
<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
<script src="http://leafletjs.com/examples/choropleth/us-states.js" type="text/javascript"></script>
<script src="http://leafletjs.com/examples/geojson/sample-geojson.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js" type="text/javascript"></script>
</body>
</html>
#map {
width: 600px;
height: 400px;
}
var map = L.map('map').setView([39.74739, -105], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
id: 'mapbox.light'
}).addTo(map);
var campusLayer = L.geoJson(campus, {
onEachFeature: onEachFeature
});
campusLayer.addTo(map);
// Define the States layer, but don't add it to the map yet
var statesLayer = L.geoJson(statesData, {
onEachFeature: onEachFeature
});
function onEachFeature(feature, layer) {
var popupContent = "";
if (feature.properties) {
if(feature.properties.popupContent) {
popupContent = feature.properties.popupContent;
} else if (feature.properties.name) {
popupContent = feature.properties.name;
}
}
layer.bindPopup(popupContent);
}
$("#chkStates").on("click", function(){
// Add the States layer to the map when the checkbox is clicked
if(statesLayer._map == undefined || statesLayer._map == null) {
statesLayer.addTo(map);
} else {
map.removeLayer(statesLayer);
}
})
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |