简体   繁体   English

渲染从Google Map Directions API获取的PHP中的编码折线

[英]Renders an encoded polyline in PHP obtained from the Google Map Directions API

I'm actually trying to render an encoded polyline obtained from the Google Map Directions API. 我实际上是在尝试渲染从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. 例如,当我请求Directions API生成从巴黎(法国)到里尔(法国)的路线时,我会得到概述多段线,这是描述该路线的编码多段线。

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. 如果我尝试使用Interactive Polyline Encoder Utility进行渲染,则折线已正确渲染。 So, The Google Map Directions API is correctly requested. 因此,正确请求了Google Map Directions API。

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. 在Chrome上,我得到了一条折线,但这绝对不是我想要的。 In Firefox, I get a JS error which is malformed Unicode character escape sequence . 在Firefox中,我收到一个JS错误,该错误是malformed Unicode character escape sequence错误的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. 我已经在这个愚蠢的问题上度过了余生,我决定提出一个问题,并在5分钟后找到解决方案。 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). 因此,如果要使用编码的折线值,则需要转义所有需要转义的字符(“)(')(\\)(NULL)。

For that in PHP, you just have to use: addSlashes . 为此,您只需使用: addSlashes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM