Leaflet Inverse Isochrones
Display inverse isochrones on a leaflet map
<!DOCTYPE html>
<html>
<head>
<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>
<script src="https://releases.targomo.com/leaflet/latest-full.min.js"></script>
<script src="../_shared/tgm-utils.js"></script>
<style>
body, html {
margin: 0;
width: 100%;
height: 100%;
}
#map {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>async function initMap() {
// Coordinates to center the map (Edinburgh, Scotland)
const center = [55.949905, -3.199814];
// Initialize map with TgmExampleUtils
const { client, map } = TgmExampleUtils.initLeafletMap({
apiKey: '__targomo_key_here__',
region: 'britishisles',
center: center,
zoom: 11
});
// you need to define some options for the polygon service
const options = {
travelType: 'bike',
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 using TgmExampleUtils
sources.forEach(source => {
TgmExampleUtils.createMarker([source.lat, source.lng], map);
});
// define the polygon overlay
const polygonOverlayLayer = new tgm.leaflet.TgmLeafletPolygonOverlay({
strokeWidth: 20,
inverse: true
});
polygonOverlayLayer.addTo(map);
// get the polygons and fit bounds using TgmExampleUtils
await TgmExampleUtils.fetchAndFitPolygons(client, sources, options, map, polygonOverlayLayer);
}
initMap();</script>
</body>
</html>
Copied to clipboard