Mapbox-GL Inverse Polygons
By leveraging turf.js, we can bring the focus to the accessible areas - 30 minute walk to Birrificio Lambrate
local_activityBuffering and simplifying GeoJSON outputs is not available for all plan levels. Check our plans for details.
<!DOCTYPE html>
<html>

<head>
    <!--  Include mapboxgl javascript and css -->
    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.0/mapbox-gl.js"></script>
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.0/mapbox-gl.css" rel="stylesheet">
    <!-- Include turf for view fitting -->
    <script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>

    <!--  Include targomo core -->
    <script src="https://releases.targomo.com/core/latest.min.js"></script>
    <style>
        body,
        html {
            margin: 0;
            width: 100%;
            height: 100%;
        }

        #map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <!--  where the map will live  -->
    <div id="map"></div>
    <script>
        // create targomo client
        const client = new tgm.TargomoClient('westcentraleurope', '__targomo_key_here__');
        // Coordinates to center the map
        const lnglat = [9.231557, 45.485672];
        
        const attributionText = `__targomo_attribution__`
        
        // add the map and set the initial center to berlin
        const map = new mapboxgl.Map({
            container: 'map',
            style: 'https://api.maptiler.com/maps/streets/style.json?key=__your_maptiler_api_key__',
            zoom: 12,
            center: lnglat,
            attributionControl: false
        })
            .addControl(new mapboxgl.NavigationControl())
            .addControl(new mapboxgl.AttributionControl({ compact: true, customAttribution: attributionText }));
        
        // disable scroll zoom
        map.scrollZoom.disable();
        
        // you need to define some options for the polygon service
        const options = {
            travelType: 'walk',
            travelEdgeWeights: [1800], // 30 minutes only
            maxEdgeWeight: 1800,
            edgeWeight: 'time',
            srid: 4326,
            simplify: 200,
            serializer: 'geojson',
            buffer: 0.002
        };
        
        const sources = [
            { lat: lnglat[1], lng: lnglat[0], id: 1 }
        ]
        
        map.on('load', () => {
            const marker = new mapboxgl.Marker()
                .setLngLat(lnglat).addTo(map);
            // call the Targomo service
            client.polygons.fetch(sources, options).then((geojsonPolygons) => {
                const inverse = turf.featureCollection([
                    // cut the polygons out of a global box
                    turf.difference(
                        turf.bboxPolygon([-180, 80, 180, -80]), // pretty much the inhabited world
                        geojsonPolygons.features[0] // we only requested one travel time
                    )
                ]);
        
                map.addLayer({
                    'id': 'polygons',
                    'type': 'fill',
                    'source': {
                        'type': 'geojson',
                        'data': inverse
                    },
                    'layout': {},
                    'paint': {
                        'fill-color': '#000',
                        'fill-opacity': .2
                    }
                });
        
                map.fitBounds(turf.bbox(geojsonPolygons), { padding: 10 });
            });
        })
    </script>
</body>

</html>
content_copy
Copied to clipboard