mapbox-gl-directions icon indicating copy to clipboard operation
mapbox-gl-directions copied to clipboard

Add a parameter to obtain geometries in GeoJSON format

Open bomanimc opened this issue 9 years ago • 3 comments

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?

bomanimc avatar Jun 15 '16 18:06 bomanimc

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();

mapsam avatar Nov 22 '16 00:11 mapsam

That was a good idea. How can I add it myself to mapbox?

Edit: I simply used geojson-polyline as an alternative. Works great.

lodi-g avatar Aug 16 '18 13:08 lodi-g

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);

Lazza avatar Dec 30 '19 20:12 Lazza