How can I get a route given by the Directions API as a full set of LatLng pairs of coordinates?

113 Views Asked by At

I want to calculate the minimum distance between a point and a polyline with JavaScript and TurfJS. I can use the pointToLineDistance function for that, as follows:

var pt = turf.point([0, 0]);
var line = turf.lineString([[1, 1],[-1, 1]]);

var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
//=69.11854715938406

However, I want the LineString ('line') to be a route given by the Directions API. Therefore, I need to get the route as a set of LatLng coordinate pairs, such as:

var line = turf.lineString([
    [-77.031669, 38.878605],
    [-77.029609, 38.881946],
    [-77.020339, 38.884084],
    [-77.025661, 38.885821],
    [-77.021884, 38.889563],
    [-77.019824, 38.892368]
]);

The set of pairs above would form this line over Washington, DC (ignore the two markers)

How can I get a route given by the Directions API as a full set of LatLng pairs?

I tried reading through the API's documentation. You can draw polylines from routes. Assuming the polylines are drawn based on the sets of coordinate pairs of a route, they must be in there, somewhere. I just couldn't find a way to get these sets from routes.

0

There are 0 best solutions below