Ruby URL编码似乎搞乱了Google Maps Static API的网址。
这是未转义的网址。
http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:orange%7Cenc:`qdlEa`zh[?@?????@???????????@????A??@???????????????A?AAA?A@CBCBC@EDE@C@C?E@C?A??AA?AAA?A@@??@A???AA????@?@@@@BBB@BDD@BBBDD??@??@B@@@BBD@B@B?D?B?DADCFCDEBGDIBIBG@EBG@E@G?EAEAECEEIGAG?GBKHEFEJEJELEJCJCJEJEJCH?@?@???@??A?C@EBG@G@E@C?GBIDKHILGR?XBLFFFBF?JCDGDMDMDSFQHWLQLSNMRSJKFI@I@KAIEMGEE@GDIJMPMRQTMRW`@MPET?LBFBDB@D?FCFGHKFOFSJQRWFKDE@EBA@EDCBE@EBEBEDEFGDCHENKJMHQ?OCEA?E@CHMJMLSLQHSLIHA@A@A@???@????A?@ADCDEDEBCBA@?BB?FADCDCBABCDEDEFEDGHGFGFEDCBCBA@C@E@GBIBGHEDAB?D@@B@D@BABCBMDMHKFCBABCDCDA@A@A@A?A@CBEDEFAD?FADADABCDE@E?IAGGGECE?I@GHCFEJEJKNMHQFMHIHCL?JBFBBF?DALKJIFIJIFIHIBC?A?A@CBEFIJ?FDL@BC@GACG?K?M?ODQDQCCODKJALDFHDDHDD@B@@BCFEDGBGBIDKBIFIFMJKHKJIRALBLDFF?DCDIFKFMFOFSFOFUFQFK@A?AD@D@DH@LGNKBGEAI@GDCDA@CACAAC@IDIDMLMHGN?J@HBDDBD?DADEFOBMBIBI@EBEBAB?D?F@FB@B?BA@E?ECGCGCGCGCE?E?G?I?GEACAA?AAC???A???A???A???A?AAAACCCAEACAAACCEAC?C?CAAAAA?A?@C@C@G?E?A???A????????A@????????????????????????A???????????????????????????????????
一旦我使用URI.encode进行编码,就无法使用Google Maps API正确解析。
这是编码的URL字符串
http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%257Ccolor:orange%257Cenc:%60qdlEa%60zh[?@?????@???????????@????A??@???????????????A?AAA?A@CBCBC@EDE@C@C?E@C?A??AA?AAA?A@@??@A???AA????@?@@@@BBB@BDD@BBBDD??@??@B@@@BBD@B@B?D?B?DADCFCDEBGDIBIBG@EBG@E@G?EAEAECEEIGAG?GBKHEFEJEJELEJCJCJEJEJCH?@?@???@??A?C@EBG@G@E@C?GBIDKHILGR?XBLFFFBF?JCDGDMDMDSFQHWLQLSNMRSJKFI@I@KAIEMGEE@GDIJMPMRQTMRW%60@MPET?LBFBDB@D?FCFGHKFOFSJQRWFKDE@EBA@EDCBE@EBEBEDEFGDCHENKJMHQ?OCEA?E@CHMJMLSLQHSLIHA@A@A@???@????A?@ADCDEDEBCBA@?BB?FADCDCBABCDEDEFEDGHGFGFEDCBCBA@C@E@GBIBGHEDAB?D@@B@D@BABCBMDMHKFCBABCDCDA@A@A@A?A@CBEDEFAD?FADADABCDE@E?IAGGGECE?I@GHCFEJEJKNMHQFMHIHCL?JBFBBF?DALKJIFIJIFIHIBC?A?A@CBEFIJ?FDL@BC@GACG?K?M?ODQDQCCODKJALDFHDDHDD@B@@BCFEDGBGBIDKBIFIFMJKHKJIRALBLDFF?DCDIFKFMFOFSFOFUFQFK@A?AD@D@DH@LGNKBGEAI@GDCDA@CACAAC@IDIDMLMHGN?J@HBDDBD?DADEFOBMBIBI@EBEBAB?D?F@FB@B?BA@E?ECGCGCGCGCE?E?G?I?GEACAA?AAC???A???A???A???A?AAAACCCAEACAAACCEAC?C?CAAAAA?A?@C@C@G?E?A???A????????A@????????????????????????A???????????????????????????????????
我也在可寻址宝石中尝试了编码。我遇到了同样的问题。如果我识别无效的URL字符并用gsub手动替换它。它似乎有效,但这似乎不是解决方案。
答案 0 :(得分:0)
试试这个:
require 'cgi'
unescaped_url = "http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:orange%7Cenc:`qdlEa`zh[?@?????@???????????@????A??@???????????????A?AAA?A@CBCBC@EDE@C@C?E@C?A??AA?AAA?A@@??@A???AA????@?@@@@BBB@BDD@BBBDD??@??@B@@@BBD@B@B?D?B?DADCFCDEBGDIBIBG@EBG@E@G?EAEAECEEIGAG?GBKHEFEJEJELEJCJCJEJEJCH?@?@???@??A?C@EBG@G@E@C?GBIDKHILGR?XBLFFFBF?JCDGDMDMDSFQHWLQLSNMRSJKFI@I@KAIEMGEE@GDIJMPMRQTMRW`@MPET?LBFBDB@D?FCFGHKFOFSJQRWFKDE@EBA@EDCBE@EBEBEDEFGDCHENKJMHQ?OCEA?E@CHMJMLSLQHSLIHA@A@A@???@????A?@ADCDEDEBCBA@?BB?FADCDCBABCDEDEFEDGHGFGFEDCBCBA@C@E@GBIBGHEDAB?D@@B@D@BABCBMDMHKFCBABCDCDA@A@A@A?A@CBEDEFAD?FADADABCDE@E?IAGGGECE?I@GHCFEJEJKNMHQFMHIHCL?JBFBBF?DALKJIFIJIFIHIBC?A?A@CBEFIJ?FDL@BC@GACG?K?M?ODQDQCCODKJALDFHDDHDD@B@@BCFEDGBGBIDKBIFIFMJKHKJIRALBLDFF?DCDIFKFMFOFSFOFUFQFK@A?AD@D@DH@LGNKBGEAI@GDCDA@CACAAC@IDIDMLMHGN?J@HBDDBD?DADEFOBMBIBI@EBEBAB?D?F@FB@B?BA@E?ECGCGCGCGCE?E?G?I?GEACAA?AAC???A???A???A???A?AAAACCCAEACAAACCEAC?C?CAAAAA?A?@C@C@G?E?A???A????????A@????????????????????????A???????????????????????????????????"
encoded_url = CGI.escape unescaped_url.force_encoding('ASCII-8BIT')