Layer control with interaction
by Muhammad Muzaffar
HTML
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.0/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js"></script>
<div id='map'></div>
CSS
#map {
width:600px;
height:400px;
}
JavaScript
// Remember to include either the Leaflet 0.7.3 or the Leaflet 1.0.0-beta1 library
var blocks1995 =
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "time": 1995, "density": 3.1, "nameB": "BlockA"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 50.0, 29.0 ], [ 50.0, 29.99],
[50.51, 29.99], [ 50.0, 29.0 ] ] ] } },
{ "type": "Feature", "properties": { "time": 1995, "density": 1.1, "nameB": "BlockB"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 50.01, 30.0 ], [ 50.52, 30.0],[50.52, 30.5] ] ] } }
]
};
var blocks1996 =
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "year": 1996, "density": 2.2, "name": "BlockA"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 50.0, 29.0 ], [ 50.0, 29.99], [50.51, 29.99] ] ] } },
{ "type": "Feature", "properties": { "year": 1996, "density": 1.2, "name": "BlockB"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 50.01, 30.0 ], [ 50.52, 30.0],[50.52, 30.5] ] ] } }
]
};
var blocks1995Layer = L.layerGroup([ L.geoJson(blocks1995)]),
blocks1996Layer = L.layerGroup([ L.geoJson(blocks1996)]);
var map = new L.map('map', {layers:[blocks1995Layer]})
.setView([29, 50], 7);
var positron = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
function colorThis(d) {
return d > 3 ? '#810f7c' :
...