Leaflet Time vs Distance Edge Weight

This code example shows you how to switch between the Time and Distance edge weight types. To show the difference in the results, a call is made to the Isochrone API each time the setting is changed and the results are displayed on a leaflet map. Time is expressed in seconds, and distance in meters. The max edge weight is 1800 (seconds/meters) for both ’time’ and ‘distance’ in this code example.
<!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>

    <link rel="stylesheet" href="https://releases.targomo.com/tgm-icons/webfont/tgm-icon.css">

    <style>
        body, html {
            margin: 0;
            width: 100%;
            height: 100%;
        }
        #map {
            width: 100%;
            height: 100%;
        }
        
        body, html,.markers {
            position: fixed; top: 10px; right: 10px;
        }
        .marker-container {
            padding: 4px 6px;
            background-color: #fff;
            display: flex; align-items: center;
            justify-content: center;
            cursor: pointer;
            border: 2px solid rgba(0,0,0,0.2);
            background-clip: padding-box;
            border-radius: 4px;
            margin-bottom: 6px;
        }
        .marker-container.active {
            background-color: yellow;
        }
        
    </style>
</head>
<body>
    
    <!--  selector used to change the routing mode  -->
    <div class="markers">
        <div onclick="setMode('time')" id="btn-time" class="marker-container active" title="union">
            <span class="tgm-icon tgm-ux-time"></span>
        </div>
        <div onclick="setMode('distance')" id="btn-distance" class="marker-container" title="average">
            <span class="tgm-icon tgm-ux-distance"></span>
        </div>
    </div>
    
    <div id="map"></div>
    <script>// Initialize map with TgmExampleUtils
        const { client, map } = TgmExampleUtils.initLeafletMap({
            ...TgmExampleUtils.PRESETS.berlin
        });
        
        const travelBreaks = [300, 600, 900, 1200, 1500, 1800];
        
        // you need to define some options for the polygon service
        const options = {
            travelType: 'bike',
            travelEdgeWeights: travelBreaks,
            maxEdgeWeight: 1800,
            edgeWeight: 'time',
            serializer: 'json'
        };
        
        // define the starting point
        const source = { id: 0, lat: 52.52, lng: 13.37 };
        
        // Add a marker for the source on the map using TgmExampleUtils
        TgmExampleUtils.createMarker([source.lat, source.lng], map);
        
        // define the polygon overlay
        const polygonOverlayLayer = new tgm.leaflet.TgmLeafletPolygonOverlay({ strokeWidth: 20 });
        polygonOverlayLayer.addTo(map);
        
        // 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 setMode(mode) {
            const selector = `btn-${mode}`;
            document.getElementsByClassName('active')[0].classList.remove('active');
            document.getElementById(selector).classList.add('active');
            options.edgeWeight = mode;
            refreshPolygons();
        }
        
        // Request polygons once immediately on page load.
        refreshPolygons();</script>
</body>
</html>
content_copy
Copied to clipboard