Leaflet Isochrones with Traffic Congestion
<!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%;
}
body, html,.congestion-mode-select-container {
position: absolute;
top: 0px;
right: 0px;
padding: 10px;
z-index: 400;
background-color: white;
}
</style>
</head>
<body>
<!-- checkbox used to change the congestion mode -->
<div class="congestion-mode-select-container">
<input type="checkbox" id="congestion-mode-select" checked="true" name="congestion"
onchange="switchmode(this);">
<label for="congestion">Congestion mode</label>
</div>
<div id="map"></div>
<script>const checkbox = document.querySelector("#congestion-mode-select");
// Coordinates to center the map (Sydney, Australia)
const center = [-33.870115, 151.208576];
// Initialize map with TgmExampleUtils
const { client, map } = TgmExampleUtils.initLeafletMap({
apiKey: '__targomo_key_here__',
region: 'australia',
center: center,
zoom: 11
});
// Define the source location which is passed into the Targomo polygon service.
const source = { id: 1, lat: -33.870115, lng: 151.208576 };
// Add marker for the source on the map using TgmExampleUtils
TgmExampleUtils.createMarker([source.lat, source.lng], map);
const travelTimes = [300, 600, 900, 1200, 1500, 1800];
// Set the traveloptions and options for the polygon service here. The 'rushHour' option will be changed everytime the checkbox is updated.
const options = {
travelType: 'car',
travelEdgeWeights: travelTimes,
maxEdgeWeight: 1800,
edgeWeight: 'time',
srid: 4326,
simplify: 200,
serializer: 'json',
rushHour: true
};
// Using the Leaflet extension we can easily visualize the returned polygons on a Leaflet map.
const polygonOverlayLayer = new tgm.leaflet.TgmLeafletPolygonOverlay();
polygonOverlayLayer.addTo(map);
polygonOverlayLayer.setStrokeWidth(20)
// Requesting polygons from the Targomo API.
async function refreshPolygons() {
await TgmExampleUtils.fetchAndFitPolygons(client, [source], options, map, polygonOverlayLayer);
}
// Update the polygons each time the selection is updated.
function switchmode(v) {
options.rushHour = v.checked;
refreshPolygons();
}
// Request polygons once immediately on page load.
refreshPolygons();</script>
</body>
</html>
Copied to clipboard