Google Directions API Encoded Polyline

天大地大妈咪最大 提交于 2019-12-04 21:43:10

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.

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.

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

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

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