Leaflet Sidebar - GeoJSON toggle
HTML
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Custom Leaflet GUI</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/leaflet-sidebar.min.css"><link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div id="sidebar" class="leaflet-sidebar collapsed">
<!-- Nav tabs -->
<div class="leaflet-sidebar-tabs">
<ul role="tablist">
<li><a href="#home" role="tab"><i class="fa fa-bars"></i></a></li>
<li class="disabled"><a href="#profile" id="toggledraw" role="tab"><i class="fa fa-pen"></i></a></li>
</ul>
</div>
<!-- Tab panes -->
<div class="leaflet-sidebar-content">
<div class="leaflet-sidebar-pane" id="home">
<h1 class="leaflet-sidebar-header">
Layers
<span class="leaflet-sidebar-close"><i class="fa fa-caret-left"></i></span>
</h1>
<span id="layercontrol"></span>
<div id="latlng"></div>
</div>
<div class="leaflet-sidebar-pane" id="profile">
<h1 class="leaflet-sidebar-header">Profile<span class="leaflet-sidebar-close"><i class="fa fa-caret-left"></i></span></h1>
</div>
</div>
</div>
<div id="map"></div>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js'></script>
<script...
CSS
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 100%;
font-family: "Roboto", sans-serif;
}
/* Leaflet Layers Control */
.leaflet-control-layers {
border-radius: none;
box-shadow: none;
}
.leaflet-control-layers-expanded {
width: 100% !important;
padding: 0px;
background-color: transparent;
border: none !important;
}
#layercontrol {
display: inline-block;
width: 100%;
padding: 10px;
background-color: transparent;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
#layercontrol .title {
font-size: 16px;
font-weight: bold;
margin-bottom: 5px;
}
/* Leaflet Draw */
.leaflet-draw {
display: none;
}
.leaflet-draw-toolbar {
display: none;
}
#latlng {
position: absolute;
right: 0px;
bottom: 0px;
font-size: 12px;
font-weight: bold;
padding: 2px 7px 1px 3px;
margin-left: auto;
margin-right: auto;
color: black;
background-color: white;
-webkit-border-top-left-radius: 5px;
-webkit-border-top-right-radius: 5px;
-moz-border-radius-topleft: 5px;
-moz-border-radius-topright: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
-webkit-box-shadow: 0px -2px 5px 0px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 0px -2px 5px 0px rgba(0, 0, 0, 0.75);
box-shadow: 0px -2px 5px 0px rgba(0, 0, 0, 0.75);
z-index: 1000;
display: none;
}
JavaScript
fetch('https://data.cityofnewyork.us/resource/ek5y-zcyf.geojson?$where=latitude is not null')
.then(function (response) {
// Read data as JSON
return response.json();
})
.then(function (data2) {
// Create the Leaflet layer for the data
var complaintLayer = L.geoJson(data2, {
// We make the points circles instead of markers so we can style them
pointToLayer: function (geoJsonPoint, latlng) {
return L.circleMarker(latlng);
},
// Then we can style them as we would other features
style: function (geoJsonFeature) {
return {
fillColor: '#0000ff',
radius: 6,
fillOpacity: 0.7,
stroke: false
};
}
});
complaintLayer.addTo(map);
});
fetch('https://data.cityofnewyork.us/resource/755u-8jsi.geojson')
.then(function (response) {
// Read data as JSON
return response.json();
})
.then(function (data3) {
// Create the Leaflet layer for the data
var nyc_taxizones = L.geoJson(data3, {
style: function (geoJsonFeature) {
return {
color: '#000000',
fillOpacity: 0.0,
opacity: 1,
stroke: true
};
}
});
// Add data to the map
nyc_taxizones.addTo(map);
});
var littleton = L.marker([40.697002, -74.024081]).bindPopup('This is Littleton, CO.'),
denver = L.marker([39.74, -104.99]).bindPopup('This is Denver, CO.'),
aurora = L.marker([39.73, -104.8]).bindPopup('This is Aurora, CO.'),
golden = L.marker([40.779002, -74.056]).bindPopup('This is Golden, CO.');
var cities = L.layerGroup([littleton, denver]);
var today = L.layerGroup([aurora, golden]);
//Init Overlays
var overlays = {
"Cities": cities,
"Today": today,
};
//Init BaseMaps
var basemaps = {
"OpenStreetMaps": L.tileLayer(
"http://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/{z}/{y}/{x}",
{
minZoom: 11,
...