1

I am trying to map routes which should match the Google Maps street network.

I know how to serialize the JSON result from the Google Maps Directions API.

I am looking now at encoding the polylines from the individual routing steps.

I have found some decoder algorithm here on stackoverflow but tested first Google's own Interactive Polyline Encoder. I would expect the encoded line to match the Google Map but it doesn't.

The screenshot shows the result of an encoded segment in the Interactive Polyline Encoder on the left and the result from a Google Maps route on the right.

enter image description here

The encoded polyline is "uxhuBxrc|QPA@?@?B?B@@B@D@HDFD@B@H@JBB@DBFJ"

Is it possible that the Algorithm in Google's Interactive Polyline Encoder is wrong?

Has anyone tested Jeffrey Sambells algorithm and knows whether the results match the results from Google's Interactive decoder?

Cheers, Dennis

EDIT: The problem is with the returned polyline segments and not the encoding algorithm. Even the sample segment which Google provides in the example response on the Directions API page fits very poorly with the underlying road geometry.

enter image description here

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
  • possible duplicate of [Get a polyline from Google maps directions V3](http://stackoverflow.com/questions/16180104/get-a-polyline-from-google-maps-directions-v3) – geocodezip Mar 14 '14 at 05:45
  • not a duplicate of aforementioned question. i have meanwhile run the algorithm to decode the polyline myself and the result is the same as shown in the interactive polyline encoder. the question is therefore why are the results from the directions api different from the polyline results shown in the google maps application? – Dennis Bauszus Mar 14 '14 at 17:23
  • because as I said in the answer (which you claim you read and isn't a duplicate) the overview_path encoded polyline is simplified. – geocodezip Mar 14 '14 at 17:44
  • i am not using the overview_path. i am digitizing the actual segment as in routes.legs.steps.polyline – Dennis Bauszus Mar 14 '14 at 17:50

4 Answers4

2

I had a hard time with this as well. The only solution I managed to come up with (works great though) is to use Google's snap to road API. Send the coordinates of your inaccurate polyline to the API and it will give back the corrected coordinates.

Hope this helps.

Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
  • 1
    Wow. Sounds cool. I stopped working with Google Maps in favour of OpenStreetMap. But I will mark this as correct answer as I believe that this will work. – Dennis Bauszus Apr 22 '16 at 15:04
  • I tried this and it works nicely, however, the snapping works only with points that are relatively close to each other (~300 meters). Directions api gives me points with distance between them more than few km when it is going through straight roads, and that's when Roads API breaks and gives bad snapping. Out of ideas how to counter it. I need directions API to return more points, but there is no such setting "maximum distance between steps" – Starwave Apr 20 '17 at 14:13
  • 1
    @Starwave Way back when I was working with Google Maps, I wrote a quick class which handles all this for you. Look at [BVAccurateRoutePlot](https://github.com/vinczebalazs/BVAccurateRoutePlot) – Balázs Vincze Apr 20 '17 at 14:19
  • I started using "OpenRoute Service" as alternative of Google Maps. It gets me the points of polyline in json format so I can use those directly in my code. Check the website: https://openrouteservice.org/dev/#/api-docs/directions/get and check this API: https://api.openrouteservice.org/directions?api_key=YOUR_API_KEY&coordinates=-77.132087,-11.9460073%7C-71.5342979,-16.4003839&profile=driving-car&geometry_format=geojson – Daniel Carpio Contreras Mar 05 '19 at 22:58
0

After a few months I finally got a reply from Google. Unfortunately they weren't able to understand the problem and help. I looked again at the decoding algorithm and I am now able to answer the question myself.

The algorithm rounds coordinates to 1e5.

Google Polyline algorithm

It will therefore be impossible to encode a polyline which fits with the rendering of the underlying road network which has a higher accuracy at the highest zoom level.

This image shows the maximum positional accuracy with coordinates rounded to 1e5.

enter image description here

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
0

enter image description here

You can also download a sample project from

https://github.com/xomena-so/so47492459

Don't forget to replace the API key with yours.

I hope this helps!

Mahendran Candy
  • 1,114
  • 17
  • 17
0

You could try this: http://zevross.com/blog/2014/09/23/convert-google-directions-to-geojson-points-or-polylines/?subscribe=success#blog_subscription-3

You could try hhe following steps (if you're using Node.js):

npm install polyline

And then:

var polyline = require('polyline');

// returns an array of lat, lon pairs
polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@')

Where the string "_p~iF~ps|U_ulLnnqC_mqNvxq`@" is extracted from the response of Google Maps API: root > routes > overview_polyline > points

enter image description here

The resulting points in a map could be shown like this:

enter image description here