简体   繁体   English

如何在Google Elevation API中使用坐标数组?

[英]How to use an array of coordinates with Google Elevation API?

I am trying to send several locations in one request to Google Elevation API, you are supposed to be able to send up to 512 locations per request. 我正在尝试在一个请求中向Google Elevation API发送多个位置,您应该能够每个请求最多发送512个位置。 Their documentation says to use: An array of coordinates separated using the pipe ('|') character: locations=40.714728,-73.998672|-34.397,150.644 but I am getting back the error: Caused by: java.net.URISyntaxException: Illegal character in query at index 99: https://maps.googleapis.com/maps/api/elevation/json?locations=51.606013718523265,-8.432384161819547|51.606031961540985,-8.432374430210215|51.60607166348032,-8.432334651837888|51.60610446039263,-8.4322494395575&key=myAPIkey It works if I just send a single point. 他们的文档说要使用:用管道字符('|')分隔的坐标数组:locations = 40.714728,-73.998672 | -34.397,150.644,但我得到的错误是:原因:java.net.URISyntaxException:非法索引99中查询的字符: https : //maps.googleapis.com/maps/api/elevation/json?locations=51.606013718523265 ,-8.432384161819547| 51.606031961540985 ,-8.432374430210215| 51.60607166348032 ,-8.432334651837888| 51.60610446039263 ,-8.4322494395575 & key如果我只发送一个点就可以。 I am told to use the pipe ('|') character yet it won't accept it. 有人告诉我使用竖线('|')字符,但不会接受。 My code is 我的代码是

 position = ellipsePositions.get(index1);              
 longitude = position.getLongitude();
 latitude = position.getLatitude();
 APIstring = latitude + "," + longitude;

 for (int index = 1; index < indexList.size(); index++)
 {
    if (ellipsePositions.get(index) != null)
    { 
       position = ellipsePositions.get(index);              
       longitude = position.getLongitude();
       latitude = position.getLatitude();
       APIstring = APIstring + "|" +  latitude + "," + longitude;
    }
 }   

and

WebResource webResource =   client.resource("https://maps.googleapis.com/maps/api/elevation/json?locations="+ APIstring + "&key=myAPIkey");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
String data = response.getEntity(String.class);

Can anyone help? 有人可以帮忙吗?

You need to URL encode any "unsafe" characters in the query string. 您需要对查询字符串中的所有“不安全”字符进行URL编码。 Per the documentation on web services 根据有关Web服务文档

Building a Valid URL 建立有效的URL

You may think that a "valid" URL is self-evident, but that's not quite the case. 您可能会认为“有效” URL是不言而喻的,但事实并非如此。 A URL entered within an address bar in a browser, for example, may contain special characters (eg "上海+中國"); 例如,在浏览器的地址栏中输入的URL可以包含特殊字符(例如“上海+中国”); the browser needs to internally translate those characters into a different encoding before transmission. 浏览器需要在传输之前在内部将这些字符转换为不同的编码。 By the same token, any code that generates or accepts UTF-8 input might treat URLs with UTF-8 characters as "valid", but would also need to translate those characters before sending them out to a web server. 同样,生成或接受UTF-8输入的任何代码都可能会将带有UTF-8字符的URL视为“有效”,但在将这些字符发送到Web服务器之前,也需要进行翻译。 This process is called URL-encoding. 此过程称为URL编码。

We need to translate special characters because all URLs need to conform to the syntax specified by the W3 Uniform Resource Identifier specification. 我们需要翻译特殊字符,因为所有URL都必须符合W3统一资源标识符规范所指定的语法。 In effect, this means that URLs must contain only a special subset of ASCII characters: the familiar alphanumeric symbols, and some reserved characters for use as control characters within URLs. 实际上,这意味着URL必须仅包含ASCII字符的一个特殊子集:熟悉的字母数字符号和一些保留的字符,以用作URL中的控制字符。

Some common characters that must be encoded are: 必须编码的一些常见字符是:

Unsafe character   Encoded value
|                  %7C

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

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