[GIS] Mapbox Directions Plugin – Get Route GeoJson

geojsonmapboxrouting

Goal: find the GeoJSON points in a web map that are closest to a directions route using turf.js.

I am using the Mapbox Directions plugin. I am following this example.

How do I work with the route that is the output of the directions request?

Is there are way to get the route line in GeoJSON format to perform typical Mapbox functions on it?

Best Answer

From the example you linked to, there is a directions layer on the map.

var directionsLayer = L.mapbox.directions.layer(directions)
.addTo(map);

The directions layer extends a leaflet LayerGroup, which from the leaflet documentation, has a function called toGeoJSON. So I would recommend trying something like:

var directions_geojson = directionsLayer.toGeoJSON();
Related Question