如何在 Google Maps Directions API URL 中指定地址

Phi*_*hil 1 google-maps

如何在 Google Maps Web Services Directions API URL 中指定地址?地图Web 服务页面指出

转换从用户输入收到的 URL 有时很棘手。例如,用户可以输入“5th&Main St.”这样的地址。一般来说,您应该从各个部分构建 URL,将任何用户输入视为文字字符。

然而这还不是很清楚。没有例子,我在网上也找不到任何例子。对于给定的示例,这是否意味着“5th&Main St.” 以下有效吗?

https://maps.googleapis.com/maps/api/directions/json?destination=5th%26Main+St.&sensor=true
Run Code Online (Sandbox Code Playgroud)

如果不是,正确的转换是什么?

谢谢阅读。

Pra*_*een 5

据我了解,您正在寻找URI encode.

在 JavaScript 中:

encodeURI用于对带有特殊字符(包括外语)的字符串进行编码。

例子:

var address =  "5th&Main St.";
var encodedAddress = encodeURI(address);
Run Code Online (Sandbox Code Playgroud)

然后在谷歌地图API中传递encodedAddress。

https://maps.googleapis.com/maps/api/directions/json?destination=encodedAddress&sensor=true
Run Code Online (Sandbox Code Playgroud)

符号等效编码的一个小列表(百分比编码):

space   %20
!       %21
"       %22
#       %23
$       %24
%       %25
&       %26     // this what happended in your case
'       %27
(       %28
)       %29
Run Code Online (Sandbox Code Playgroud)

有一次我在做的时候遇到了一个问题geocoding via C#

我使用地址进行了类似的 URI 编码HttpUtility.UrlEncode(),然后将其传递给 Google API,正如我上面提到的。每种语言都有自己的编码技术,但输出是相同的。

希望你能理解。