Google directions api overview polyline not workin

2019-01-20 18:32发布

问题:

I'm trying to show a path from the google maps directions api on a google maps static map. The url for the directions api call looks like this:

https://maps.googleapis.com/maps/api/directions/json?origin=lübeck&destination=hamburg&key=MY_API_KEY

I get a valid json response containing all the informations. I then want to show the encoded polyline for the path overview on a static map with this api call:

https://maps.googleapis.com/maps/api/staticmap?sensor=false&size=860x600&path=enc:THE_ENCODED_POLYLINE

The static map won't show the path. I suspect that the encoded polyline returned by the directions api is malformed or incomplete. If enter the returned encoded polyline into Googles Interactive Polyline Decoder it automatically add a '@' character at the end. The resulting polyline will show up on a static map, but it is not matching the directions.

I did the exact work flow with path between different cities and had no problems at all. Does anybody has any ideas what's wrong with this example.

Thanks for any replies.

回答1:

The encoded "overview_polyline" that I get from that directions response:

orwgI}ef`AiDyA{EiCkCoBgCqAeE_CgAc@cBUaBKi@QDLBd@Al@DnBHf@zBbAtCz@t@f@D~DAtCG~NChJEvHZAlAGlCo@nDs@bCYx@IhA[@^?x@?\\k@|BJfK`@dIWvDg@fBu@|@kB`BiIxK{BjDkDvHwBlDaCtCmNxLsFhFiBmFoCeIIaCS}@k@M[\\Ov@m@pA{CbCiFrE_CrCeD|GyA|E{@hCWL}@[iCmB[k@Q{@?aAPw@Zi@d@Mj@Nh@TzIpJtG`KpGrLLRbBpDnFvOpLz^lBdGvK~\\|Pfi@bUrs@vZxdAnJr[rK~Zjc@lnApeApxCpNh^|Ox_@rT`i@`JfWxGxT|EnRxOzp@hHl[nDrUpBxT|@nUX~d@v@|zBN|Lh@lQpBd[jB|PnAzIhDhRtCfMbExN~HhTpDbInEpIdNhTxTh\\~FxKbGpNtNr]jGpOfu@fjBpIxQvHrLtSrYhYha@vPfVnFtIvDhIzTjg@fFjLdJjSp[|r@r@pAhElHhFjHtHzJxGzI|Wr]bOxRjSrWnF|F`GpFrRlP~]nZj]vYveAl}@|j@te@dFhEpD`CjKtFbP~HdZ|NpYjNt_@lRj_@zUn~@fk@~{@zi@xmAhv@lFnD`FfE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