我使用google.maps.geometry.encoding.encodePath()
编码了多条折线的路径,并希望将它们存储在MySQL表中。
问题:在将它们存储到表格中之前,我绘制了折线,如下图所示。然而,在将它们存储到表中,检索它们并再次绘制之后,路径似乎已经改变了!发生了什么事?
其他信息:我注意到如果我要将新编码的路径写入console.log
,请从控制台日志中复制并使用此复制的路径绘制新的折线,折线在某些区域变形,就像使用从数据库中检索到的编码路径后发生的那样!
使用新编码路径的折线
JS代码
encoded_path = google.maps.geometry.encoding.encodePath(path);
console.log(encoded_path);
decoded_path = google.maps.geometry.encoding.decodePath(encoded_path);
var polyOptions = {
strokeColor: "#970E04" ,
strokeOpacity: 1.0 ,
strokeWeight: 2 ,
path: decoded_path ,
clickable: false,
map: map
}
polyline = new google.maps.Polyline(polyOptions);
来自console.log的编码路径
axwaGtbcqL
BgArBmAjCcBlBcCnBeChB_BrAmAxByBjCoCkAqBmByCu@qA[i@~BmAdAq@FEzAqAvAsA|@{@DCr@{@DIjAuA|@cAz@}@~AeB\[LZxAjDJINM|AkAxAxCfAw@g@jAeBpEOr@Ox@CTeAlMM~AYhDc@pE_AtHGj@m@
了Gm @ DHA @ hFaAbM_AfJ] jDAlA {HHV @ eAdFy @ bCcBpEeBzDyArDu @E_AnFs@|Fq@fGKtKaEqCgBmB_GmFcJaM}@nBg@BbA}CpAaEvAoEnGyOvDuPDSPYAIIMMMGOMMIIKKKQIMIKIKa@gAQ_@Q[CCY_@OSQYQQOOSMSOa@]g@g@UWSUYm@Uk@]qAKe@Ia@I_@Iy@Es@C_AAw@A]Ca@G[Qy@GSKa@M]M_@Qa@M]IOGKQSGIIG}@w@{AgAiAaAWSUOWOQIMEKCICOGMCKEQC_DuA?GbByB\_@~GaIxCgDl@s@bBkBnBcBzCqC
艾@ PCMA ^ MAD @ QA
使用从数据库检索的路径的折线
存储在VARCHAR(255)
列
使用Navicat复制
axwaGtbcqL
BgArBmAjCcBlBcCnBeChB_BrAmAxByBjCoCkAqBmByCu@qA[i@~BmAdAq@FEzAqAvAsA|@{@DCr@{@DIjAuA|@cAz@}@~AeB[LZxAjDJINM|AkAxAxCfAw@g@jAeBpEOr@Ox@CTeAlMM~AYhDc@pE_AtHGj@m@
了Gm @ DHA @ hFaAbM_AfJ] jDAlA {HHV @ eAdFy @ bCcBpEeBzDyArDu @`E_AnFs @ | @ Fq上fGKtKaEqCgBmB_GmFcJaM} @
使用从VARCHAR(1000)
存储在VARCHAR(1000)
列
使用Navicat复制
axwaGtbcqL
BgArBmAjCcBlBcCnBeChB_BrAmAxByBjCoCkAqBmByCu@qA[i@~BmAdAq@FEzAqAvAsA|@{@DCr@{@DIjAuA|@cAz@}@~AeB[LZxAjDJINM|AkAxAxCfAw@g@jAeBpEOr@Ox@CTeAlMM~AYhDc@pE_AtHGj@m@
了Gm @ DHA @ hFaAbM_AfJ] jDAlA {HHV @ eAdFy @ bCcBpEeBzDyArDu @E_AnFs@|Fq@fGKtKaEqCgBmB_GmFcJaM}@nBg@BbA}CpAaEvAoEnGyOvDuPDSPYAIIMMMGOMMIIKKKQIMIKIKa@gAQ_@Q[CCY_@OSQYQQOOSMSOa@]g@g@UWSUYm@Uk@]qAKe@Ia@I_@Iy@Es@C_AAw@A]Ca@G[Qy@GSKa@M]M_@Qa@M]IOGKQSGIIG}@w@{AgAiAaAWSUOWOQIMEKCICOGMCKEQC_DuA?GbByB\_@~GaIxCgDl@s@bBkBnBcBzCqC
艾@ PCMA ^ MAD @ QA
通过解码写入console.log的编码路径
来折线
JS代码
//encoded path copied from console.log output from the very first code in post
encoded_path = 'axwaGtbcqLBgArBmAjCcBlBcCnBeChB_BrAmAxByBjCoCkAqBmByCu@qA[i@~BmAdAq@FEzAqAvAsA|@{@DCr@{@DIjAuA|@cAz@}@~AeB\[LZxAjDJINM|AkAxAxCfAw@g@jAeBpEOr@Ox@CTeAlMM~AYhDc@pE_AtHGj@m@Gm@dHa@hFaAbM_AfJ]jDAlA{Hhv@eAdFy@bCcBpEeBzDyArDu@E_AnFs@|Fq@fGKtKaEqCgBmB_GmFcJaM}@nBg@BbA}CpAaEvAoEnGyOvDuPDSPYAIIMMMGOMMIIKKKQIMIKIKa@gAQ_@Q[CCY_@OSQYQQOOSMSOa@]g@g@UWSUYm@Uk@]qAKe@Ia@I_@Iy@Es@C_AAw@A]Ca@G[Qy@GSKa@M]M_@Qa@M]IOGKQSGIIG}@w@{AgAiAaAWSUOWOQIMEKCICOGMCKEQC_DuA?GbByB\_@~GaIxCgDl@s@bBkBnBcBzCqCAi@pCmA^mAd@qA';
decoded_path = google.maps.geometry.encoding.decodePath(encoded_path);
var polyOptions = {
strokeColor: "#970E04" ,
strokeOpacity: 1.0 ,
strokeWeight: 2 ,
path: decoded_path ,
clickable: false,
map: map
}
polyline = new google.maps.Polyline(polyOptions);
路径也无法正常工作
我注意到这些编码字符串中间有换行符?此外,编码的字符串将代码行拆分为2行,导致编辑器中的错误检查系统显示警报。
JS代码(格式不正确)
encoded_path ='{traGxcspLaIkFvAwH} BgA {EzEjCgJmEwBmDvGoHpAkLQpAqGyF_CbCuH ^} FlDwEo CYN @ÿ@ G [基{C @ WB @ E
@sG@kHgEaBiJ}EiHzMwLdFga@{U{KoZoZhH{e@hh@~RlBgJ{AgMrAkQrD[rCl@pNmAhJeCgDhQdGlItBpQbTb@
{EdQoBtMaJzSoE {1}}} RlGdFtCuMpEzAbS [QGE {@ dOsEnGjZhInDrHwIjEdH_OtdAf @〜WnM`F〜AyN_DgEzh @ nKgTvVbBrNoZvB_SvLwGfJwT〜瓦特@ WG?gBzB?xFdAt @ XFN ?@ bBve @ nPoKv [kGwBsCdGoCtBwFoBwCfFcBvBoArDsC] {AXA';
编码路径
{traGxcspLaIkFvAwH} BGA {EzEjCgJmEwBmDvGoHpAkLQpAqGyF_CbCuH ^} FlDwEo CYN @ÿ@ G [基{C @ WB @ E
f@bMzLlHRj@lHrFhCnHrNvAk@qBeOpAcAlRvCfEgQt@oB_JeBmTmL}DqJl@qHbCmEzFc]xr@nT|IoKf@
{EdQoBtMaJzSoE {1}}} RlGdFtCuMpEzAbS [QGE {@ dOsEnGjZhInDrHwIjEdH_OtdAf @〜WnM`F〜AyN_DgEzh @ nKgTvVbBrNoZvB_SvLwGfJwT〜瓦特@ WG?gBzB?xFdAt @ XFN @ bBve @?nPoKv [kGwBsCdGoCtBwFoBwCfFcBvBoArDsC] {AXA
答案 0 :(得分:2)
encodePath()
返回的字符串文字中的斜杠在写入之前被解释,无论是写入屏幕还是数据库。
我要做的就是尽早逃避斜线:
encoded_path = google.maps.geometry.encoding.encodePath(path);
encoded_path = encoded_path.replace(/\\/g,"\\\\");
这应确保写入数据库(或日志)的字符串是正确的,并且您不必在读取时执行任何特殊操作。
或者,您可以在将字符串传递到另一个媒介之前转义斜杠,例如在AJAX调用之前,或者在注释中提到的console.log()
方法调用中。