At work, we needed a simple way to buffer a polyline in order to search for stuff along the route from A to B. I’ll explain how we used Google’s Maps API and JSTS in order to achieve this easily.
The first thing we had to do was to “transform” each element in the
overview_path (which the
DirectionsService returns) to GeoJSON, because that’s what JSTS understands.
The next step was getting
overviewPathGeo into JSTS and letting it do the work of buffering the line.
polygon variable now contains a polygon that neatly fits around the
overviewPath, with a distance (buffer size) of roughly 10km.
Since it is nested, you need to call
polygon.coordinates in order to get back the coordinates of the polygon (as GeoJSON).
You could then use the the coordinates to draw the generated polygon on the map (along with the route), in order to produce something like this: