Google Directions API Encoded Polyline

梦想的初衷 提交于 2019-12-12 09:14:41

问题


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.

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.


回答1:


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.




回答2:


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.




回答3:


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!




回答4:


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

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



来源:https://stackoverflow.com/questions/22394224/google-directions-api-encoded-polyline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!