Add a parameter to obtain geometries in GeoJSON format
Right now, it seems all of the routes come back in Polyline format automatically. Can you add a way to have this set to GeoJSON through a parameter?
Hi @bomanimc! Apologies for the delayed response here. I think this would be a great feature.
I have a proof of concept going over at the return-geojson branch, which adds a new method to the directions plugin called getGeoJSON() which returns a FeatureCollection with 1 or more features depending if the directions API returns alternative routes or not.
Example usage (with new 3.x API):
var directions = new MapboxDirections( ... );
map.addControl(directions);
// add an origin and destination
var geojson = directions.getGeoJSON();
That was a good idea. How can I add it myself to mapbox?
Edit: I simply used geojson-polyline as an alternative. Works great.
Yay! 😄
I confirm geojson-polyline seems to work very well, but it might be a bit hard to understand.
For those who do not want spending a lot of time to figure it out, you have to do:
var encoded = {
'type': 'LineString',
'coordinates': route.geometry,
};
var decoded = GeoJSONPolyline.geojson(encoded);