Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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!

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top