简体   繁体   English

谷歌地图 API 请求

[英]Google Maps API request

Im doing a project where i wish to send a request to google maps.我正在做一个项目,我希望向谷歌地图发送请求。 i need to receive from google a path from one location to another.我需要从谷歌接收一条从一个位置到另一个位置的路径。 This goes in the Web Services area..这在 Web 服务区域中。

It must be done in java.它必须在java中完成。

From what i read at the google maps api description the google response to any request is in XML format.根据我在 google maps api description 中阅读的内容,google 对任何请求的响应都是 XML 格式的。

There's no problem there because for that we can use XPath and any other tool to do the parsing.那里没有问题,因为为此我们可以使用 XPath 和任何其他工具来进行解析。

So what i need is only how can i send this request to the google API in java?所以我需要的只是如何将此请求发送到 java 中的 google API? If you could explain each step it would be great..如果你能解释每一步就太好了..

Thank you for your pacience :)谢谢你的耐心:)

Well I'm working on a simular project (using Mapquest instead of Google).好吧,我正在做一个模拟项目(使用 Mapquest 而不是 Google)。 I just used a HttpConnection like this:我只是使用了这样的 HttpConnection:

String requestString = buildURL("Washington", "New York");
URL url = new URL(requestString);
HttpsConnection conn = (HttpsConnection) url.openConnection();
InputStream stream = conn.getInputStream();

Just use the normal request url that the API provides you.只需使用 API 为您提供的普通请求 url。 Build the URL with parameters like origin and destination using a StringBuilder eg使用 StringBuilder 构建带有 origin 和 destination 等参数的 URL,例如

public String buildURL(String origin, String destination)
{
StringBuilder urlBuilder = new StringBuilder();
urlBuilder("https://maps.googleapis.com/maps/api/directions/json?");
urlBuilder("origin=");
urlBuilder(origin);

urlBuilder("&destination=");
urlBuilder(destination);

urlBuilder("&key=");
urlBuilder(MY_KEY);

return urlBuilder.toString();
}

Last step is passing the stream to your XMLParser.最后一步是将流传递给您的 XMLParser。 You could also do a response code check on your Connection to handle errors like bad request.您还可以对 Connection 进行响应代码检查以处理错误请求等错误。

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

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