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

时间:2015-04-02 17:00:25

标签: java google-maps

我正在尝试将一个请求中的多个位置发送到Google Elevation API,您应该每个请求最多可以发送512个位置。他们的文档说要使用: 使用管道(' |')字符分隔的坐标数组: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=myAPIkey 如果我发送一个点,它就可以工作。我被告知使用管道(' |')但它不会接受它。我的代码是

 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;
    }
 }   

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);

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要对查询字符串中的任何“不安全”字符进行URL编码。根据{{​​3}}

  

建立有效网址

     

您可能认为“有效”的网址是不言而喻的,但事实并非如此。例如,在浏览器的地址栏中输入的URL可能包含特殊字符(例如“上海+中国”);浏览器需要在传输之前将这些字符内部转换为不同的编码。出于同样的原因,任何生成或接受UTF-8输入的代码都可能将UTF-8字符的URL视为“有效”,但在将它们发送到Web服务器之前还需要翻译这些字符。此过程称为URL编码。

     

我们需要翻译特殊字符,因为所有URL都需要符合W3统一资源标识符规范指定的语法。实际上,这意味着URL必须仅包含ASCII字符的特殊子集:熟悉的字母数字符号,以及一些用作URL中控制字符的保留字符。

     

必须编码的一些常见字符是:

Unsafe character   Encoded value
|                  %7C