Google Static Maps API with encoded polyline
M

1

5

I have made a request to google directions API and recived the following JSON directions:

(file to large to copy here) https://gist.githubusercontent.com/crooksey/9930819/raw/947e4d17a93ca94b78216d92fbd94b281966dede/directions_api

At the bottom, I get the encoded polyline data:

ecduHxqrE|RfS{SlW_w@zDc{@ig@cVlK}z@j}Aog@z{AuGbdDibAfkGaBpcG}~@vrGy_AhhCoi@feB{bBxu@kxAsSus@|eAcLdyBci@dcByi@ly@`GrgC_S~d@e\\wBugA_uBwuAusAoaCidAs~Dq{DiaAu`CuwAweAevBi@}dDwgCsrAwfBuqFkmBwzEu~DenAa|Bi_BcuDmrAghF_{B_hGa{@chAocAl[m_@pXwk@{z@}Zc~A_hAmn@mdAinBg_@eD{o@moA{_@kz@sn@ca@oWmcCoQsUue@`De{@yH{oA~i@meAliA}wAa]ij@qg@}o@w_@yVrT_k@|Y{oCi`@e_@RqEjOvUx\\`TjDv@yi@ttA~Gr}@`Fxi@ad@v]yJ|gAxlAjsAz_@|l@oYzbBatAn_CdJdSv_B~f@rlAhk@`j@f_@`rAhpAfs@f`@`kAfg@`Nr_AbgBtNt~@nb@vRlc@gXry@}Wd^iIdv@zfCvgCj{GlcAzxEnkDfkGlgGtpE|eEf{AvzBx{BzvA|_B~\\yAjyBxJzwA|wA~_AznBlgAt_AbhBpsB`xChzAxsA|uAxVnxBnPb}AzCx`EnhA`qDwCnpG~SvrCbmA|lCd{B~jFdZzbCn{@b`EhqA|aBb_Ad}BdkBxjEx[vgC|HluEfUnkBtfB`eF~mAzjD`V~q@y_@pnEtv@byChn@tv@`dBjoFz_@KdbElc@f]|My@bc@da@mKptA`Nhj@aq@fHlAbK~M?Z_UyOw^ji@su@Ju|@`qEX`cBnw@|lAsRjlD_q@~nHgA`vH}m@hvEs]gL~AzkCt[hiCcJlvFuAzzE`a@jbDnlBxyG||C|~B`d@juCaiAf`DoQf`HhS|jBLpmBuLd`Dma@zsBc[ftGcx@t|H{TnyGxTzdA_Bn[kPysBhZm`HvvAk`Rdb@ehBoBypCdF}hB_YgcAzMqx@nEohGriAi~Ca_@mpCopCkoBwpC_iIaKegKlIu{EuX_{CgD{pBs^oXuu@kpEqZmzDymBmwDt`@qkEhEmzCjCiyBqSonCmKm|@rRweC{R}cBw{@{fDeo@}u@q|@g}Cx_@quEqdBcyEs}Ba_HoQ{dJysDyvIiiBejDup@e{D{mAuxDes@a_BcoBkdGgEweJp|AkTl{@u_@lg@_{Afp@suDzBmgAmLyp@qw@sx@wdAafA_~BukA`bAmeBfl@orEtn@}f@xkAbVptAgk@xv@{}A|p@cyAhjAsiHpEkyFbaAceHvLe~Dli@a~Ary@eeBx}@lUbb@jVzf@_NhKs^yH{I

Now In theory I should be able to display this route on a map, as easy as..

https://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:10%7Ccolor:orange%7Cenc:ecduHxqrE|RfS{SlW_w@zDc{@ig@cVlK}z@j}Aog@z{AuGbdDibAfkGaBpcG}~@vrGy_AhhCoi@feB{bBxu@kxAsSus@|eAcLdyBci@dcByi@ly@`GrgC_S~d@e\\wBugA_uBwuAusAoaCidAs~Dq{DiaAu`CuwAweAevBi@}dDwgCsrAwfBuqFkmBwzEu~DenAa|Bi_BcuDmrAghF_{B_hGa{@chAocAl[m_@pXwk@{z@}Zc~A_hAmn@mdAinBg_@eD{o@moA{_@kz@sn@ca@oWmcCoQsUue@`De{@yH{oA~i@meAliA}wAa]ij@qg@}o@w_@yVrT_k@|Y{oCi`@e_@RqEjOvUx\\`TjDv@yi@ttA~Gr}@`Fxi@ad@v]yJ|gAxlAjsAz_@|l@oYzbBatAn_CdJdSv_B~f@rlAhk@`j@f_@`rAhpAfs@f`@`kAfg@`Nr_AbgBtNt~@nb@vRlc@gXry@}Wd^iIdv@zfCvgCj{GlcAzxEnkDfkGlgGtpE|eEf{AvzBx{BzvA|_B~\\yAjyBxJzwA|wA~_AznBlgAt_AbhBpsB`xChzAxsA|uAxVnxBnPb}AzCx`EnhA`qDwCnpG~SvrCbmA|lCd{B~jFdZzbCn{@b`EhqA|aBb_Ad}BdkBxjEx[vgC|HluEfUnkBtfB`eF~mAzjD`V~q@y_@pnEtv@byChn@tv@`dBjoFz_@KdbElc@f]|My@bc@da@mKptA`Nhj@aq@fHlAbK~M?Z_UyOw^ji@su@Ju|@`qEX`cBnw@|lAsRjlD_q@~nHgA`vH}m@hvEs]gL~AzkCt[hiCcJlvFuAzzE`a@jbDnlBxyG||C|~B`d@juCaiAf`DoQf`HhS|jBLpmBuLd`Dma@zsBc[ftGcx@t|H{TnyGxTzdA_Bn[kPysBhZm`HvvAk`Rdb@ehBoBypCdF}hB_YgcAzMqx@nEohGriAi~Ca_@mpCopCkoBwpC_iIaKegKlIu{EuX_{CgD{pBs^oXuu@kpEqZmzDymBmwDt`@qkEhEmzCjCiyBqSonCmKm|@rRweC{R}cBw{@{fDeo@}u@q|@g}Cx_@quEqdBcyEs}Ba_HoQ{dJysDyvIiiBejDup@e{D{mAuxDes@a_BcoBkdGgEweJp|AkTl{@u_@lg@_{Afp@suDzBmgAmLyp@qw@sx@wdAafA_~BukA`bAmeBfl@orEtn@}f@xkAbVptAgk@xv@{}A|p@cyAhjAsiHpEkyFbaAceHvLe~Dli@a~Ary@eeBx}@lUbb@jVzf@_NhKs^yH{I&sensor=true

But alas, this does not work, anyone have any ideas as to why? It should be a fairly basic exercise. As it's just using googles own API.

Maliamalice answered 3/4, 2014 at 13:28 Comment(12)
Well that's a good question and I can't figure out why this doesn't work :(Dimphia
I'm not sure if URL size limit is reached. Using just the first 8 points I got map of Portsmouth harbour with polyline. In that case URL was very short comparing to original one.Gladis
No its not the size limit, as I can increase it with random locations and it reaches the size length. But not as it is.Maliamalice
Right. This URL is around 1300 characters. I think under 2000, you should be good to go. And yes, removing a few points at the end gives you the map and polyline.Dimphia
How can I trim this consistently in a production environment then? Can you post the working URL?Maliamalice
Well the thing is that you should not need to trim it... That one works: goo.gl/6YJ1pYDimphia
possible duplicate of Directions API overview_polyline doesn't have correct pointsGladis
Removing one '\' from '...\\wBugA' it seems produce whole polyline. Otherwise it breaks with 96 coordinate points.Gladis
@AntoJurković That's right. It works, but it doesn't seem to be the same route at all if you compare to my trimmed URL. Strange. I double checked the doc and it clearly states that URLs should be under 2048 characters, which is the case here. And removing all double backslashes gives me yet another route.Dimphia
Do you get the expected route using developers.google.com/maps/documentation/utilities/… ?Dimphia
No, the route is completely wrong.Maliamalice
Removing the "/" like you said works: goo.gl/pKzlGBMaliamalice
M
10

It seems the polyline overview used a lot of "\\" which causes the problem, so I replaced all instance of "\\" with "\".

Used python replace on the string to sort it.

string_new = string.replace("\\\\", "\\")
Maliamalice answered 4/4, 2014 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.