Leaflet Isochrone Colors Legend
Use polygon options to build custom legend
<!DOCTYPE html>
<html>
<head>
<!-- Include leaflet javascript and css -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet-src.js" crossorigin="" type="text/javascript"></script>
<!-- Include targomo core -->
<script src="https://releases.targomo.com/core/latest.min.js" type="text/javascript"></script>
<script src="https://releases.targomo.com/leaflet/latest-full.min.js" type="text/javascript"></script>
<style>
body,
html {
margin: 0;
width: 100%;
height: 100%;
}
#map {
width: 100%;
height: 100%;
}
.legend {
position: fixed;
top: 10px;
width: 100%;
z-index: 1000;
display: flex;
justify-content: center;
}
.legend .container {
width: 75%;
max-width: 400px;
display: flex;
justify-content: center;
}
.legend .container .cell {
text-align: center;
padding: 5px 0;
color: white;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
min-width: 60px;
}
</style>
</head>
<body>
<!-- where the map will live -->
<div id="map"></div>
<div class="legend">
<div class="container"></div>
</div>
<script>
async function initMap() {
// create targomo client
const client = new tgm.TargomoClient('easterneurope', '__targomo_key_here__');
// define the basemap
const tileLayer = new tgm.leaflet.TgmLeafletTileLayer(client, 'Dark');
// Coordinates to center the map
const center = [59.450954, 24.728370];
// define the map
var map = L.map('map', {
layers: [tileLayer],
scrollWheelZoom: false
}).setView(center, 13);
// set the attribution
const attributionText = '<a href="https://www.targomo.com/developers/resources/attribution/" target="_blank">© Targomo</a>';
map.attributionControl.addAttribution(attributionText);
// polygons time rings
const travelTimes = [300, 600, 900, 1200, 1500, 1800];
// you need to define some options for the polygon service
const options = {
travelType: 'walk',
travelEdgeWeights: travelTimes,
maxEdgeWeight: 1800,
edgeWeight: 'time',
serializer: 'json'
};
// define the starting point
const sources = [{ id: 0, lat: center[0], lng: center[1] }];
// Add markers for the sources on the map.
sources.forEach(source => {
L.marker([source.lat, source.lng]).addTo(map)
});
// define the polygon overlay
const polygonOverlayLayer = new tgm.leaflet.TgmLeafletPolygonOverlay({ strokeWidth: 30 });
polygonOverlayLayer.addTo(map);
// iterate the colors assigned to the polygons to build legend
Object.keys(polygonOverlayLayer.options.colors).forEach(time => {
const color = polygonOverlayLayer.options.colors[time];
const label = (time / 60) + 'min';
const target = document.querySelector('.container');
const cell = document.createElement('div');
cell.classList.add('cell');
cell.style.backgroundColor = color;
cell.innerText = label;
target.appendChild(cell);
})
// get the polygons
const polygons = await client.polygons.fetch(sources, options);
// add polygons to overlay
polygonOverlayLayer.setData(polygons);
// calculate bounding box for polygons
const bounds = polygons.getMaxBounds();
// zoom to the polygon bounds
map.fitBounds(new L.latLngBounds(bounds.northEast, bounds.southWest), 100);
}
initMap();
</script>
</body>
</html>
Copied to clipboard