I'm trying to use the side-by-side plugin downloaded from https://github.com/digidem/leaflet-side-by-side. I want to create side-by-side GeoJSON layers to compare the cancer rates between the two years and to show the legend for each layer, but I don't know what am I doing wrong because only one layer is displayed on both sides or they overlap. I guess I should change some of the code in the js file, but I don't know what.
<!DOCTYPE html>
<html>
<head>
<title>Cancer Rates Iowa</title>
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui' />
<script src='http://cdn.leafletjs.com/leaflet/v1.3.1/leaflet.js'></script>
<script src="leaflet-side-by-side.js"></script>
<link href='http://cdn.leafletjs.com/leaflet/v1.3.1/leaflet.css' rel='stylesheet' />
<script src="Cancer_IA.geojson"></script>
<style>
body {
padding: 0;
margin: 0;
}
#map{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.legend {
text-align: left;
line-height: 40px;
color: #555;
}
.legend i {
width: 40px;
height: 40px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([42.0667, -93.4], 8);
var basemap = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
function getColor(d) {
return d > 545.0 ? '#191710' :
d > 497.0 ? '#4c4730' :
d > 462.0 ? '#665e40' :
d > 406.0 ? '#7f7650' :
d > 316.0 ? '#b2a570' :
'#e5d590';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: getColor(feature.properties.IA_2000_Ra)
};
}
var osmLayer = L.geoJson(cancer, {
style: style,
}).addTo(map);
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [316.0, 406.0, 462.0, 497.0, 545.0],
labels = [],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + getColor(from + 1) + '"></i> ' +
from + (to ? '–' + to : '+'));
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
function getColor1(d) {
return d > 574 ? '#4D0026' :
d > 504.0 ? '#48006a' :
d > 446.0 ? '#dd3497' :
d > 353.0 ? '#fcc5c0' :
d > 308.0 ? '#fef3f2' :
'#fff7f3';
}
function style1(feature1) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: getColor1(feature1.properties.IA_2016_Ra)
};
}
var stamenLayer = L.geoJson(cancer, {
style: style1,
}).addTo(map);
var legend1 = L.control({position: 'bottomleft'});
legend1.onAdd = function (map) {
var div1 = L.DomUtil.create('div', 'info legend'),
grades1 = [308.0, 353.0, 446.0, 504.0, 574.0],
labels1 = [],
from1, to1;
for (var i = 0; i < grades1.length; i++) {
from = grades1[i];
to = grades1[i + 1];
labels1.push(
'<i style="background:' + getColor1(from + 1) + '"></i> ' +
from + (to ? '–' + to : '+'));
}
div1.innerHTML = labels1.join('<br>');
return div1;
};
legend1.addTo(map);
L.control.sideBySide(stamenLayer, osmLayer).addTo(map);
</script>
</body>
</html>