I’m actually trying to render an encoded polyline obtained from the Google Map Directions API.
For example, when I request the Directions API for generating an itinerary from Paris (France) to Lille (France), I get an overview polyline which is an encoded polyline describing the itinerary.
The encoded polyline value is:
<?php
$encodedPolyline = '_meiHkmjMuF|X{LqFqm@}XaUqK_PuHyE`AsFxEaHhGwAA{@eFkAwHeFsBiIoDkMiDqIOu\wAoa@|BkZQoQzB}tBnHqWdA_IyD_GuX}DcVeMmu@sD}_@wAst@i@ag@_Fcn@iCoQcQwlAuS}vAmJs`@miBgeEyeAijB{Ymg@qe@my@yVq`@uj@eo@gEqEm[m^_a@s]yEoCoNaGse@mRcXwK}aAw`@_~@iXuYgKoZyNmM}Cam@qMe_@yLuP}Aq{@zB{l@zBy_BrF_Le@k_AuKw`@iOs^iI{g@}Fs[qPmZo^qwCc~D}KcIiPmFgi@sDeTmKiPuKgTsFwOAcOrC{PtEqOj@gTkE{XqP_`@wWsUiVcZoi@kIo^iQw_@um@odB}QaZkVaW{o@yt@_v@_rAs\}Z{qB}hAeSmGsOo@mHj@kXrHk]rL_\jFsa@hG}JdDyOfLsIbGgVfFk]cE}^iH_l@yQayCodA_iAuPaoAmAuo@~GkvAv^if@rMs[hFoj@m@asDgN}e@{Hqa@iOupA{p@{[}K_a@sFqkBsUc`CkZqgAeNon@oPaiBkg@owB{w@oeAs`@s`AkZ}h@}Nwk@uJ_cBwAep@gBeu@iMedAqVigB_k@qi@qO_YkBo\Vw|BnBiVU_TwCuaAi^qx@i\c|@iXebCoq@o`Bq\ykAeVk\gJub@sX{a@c`@oVyOe]qN}pAyg@_l@cS}h@}HqgAqKwhBcQcn@}Fs_AkLab@wRua@qYue@eRu_@yD{f@{BuvAgGsp@d@}l@_Cgc@oDsn@eHiSoHug@i[eh@qV}u@i\u|@g]q[yDup@_Eyg@}Psa@mRcYsIae@{BqOjAq_@nKur@d^sl@fMkyBzTee@lEm`@l@ek@{D_k@aBmrAKaeBMobBoC{pCcIif@yAqp@K}TzAui@`L_o@zRm}@bYwaBph@oj@vMaa@lDuk@}Awf@iKoo@cY{gC_uAybB_~@gw@mb@sXcReOoOkYa]mXc\{vAwaB_c@mh@mmAecC{Py[s^mg@k`@u^mQmMm_Acr@{TqOwU}JuNaDce@wDmrAmI_r@oEw^^mY`GcPzGoIdFgl@hd@e\|PuObEuW`CgR_@uYeGwkBgf@qi@qOk^{SoUsVep@gnAwXal@uU}p@o^ecAcNcVs\g_@_hAcs@ghB_hA{hAqt@_~As{AyfAseA_aA_~@qI}HuJuDaOUmTzI{]|j@qXze@aNfUkJlSrA|`@KpXy@dv@eDIFfCi@zEU`JcAtb@iAfUaEwAqJoHuIcIsOyJkNqKaBPoCzC';
If I try to render it with the Interactive Polyline Encoder Utility, the polyline is correctly rendered. So, The Google Map Directions API is correctly requested.
To render the encoded polyline, I use this code:
<?php
$polyline = sprintf('var polyline = Polyline({"map":map,"path":google.maps.geometry.encoding.decodePath("%s")});', $encodedPolyline);
On Chrome, I get a polyline but it’s absolutely not what I want.
In Firefox, I get a JS error which is malformed Unicode character escape sequence.
That’s sure, the problem comes from the rendering but I don’t know what’s wrong…
I have spent my afternoom on this stupid issue, I decide to create a question & 5 minutes later, I find the solution. It’s a joke !
So, If you want to use the encoded polyline value, you need to escape all chars which need to be escaped (“)(‘)(\)(NULL).
For that in PHP, you just have to use:
addSlashes.