我知道如何以老式的方式进行调用,我基本上只是构造了一个字符串,它是请求的完整url,然后发送该字符串以获取响应。
然而,谷歌有这个java库(https://developers.google.com/api-client-library/java),我想使用它。然而,我在他们的github示例页面上找不到有关如何使用地理编码或时区API的示例。
他们有哪些其他服务的例子看起来很复杂。我只想看一个基本的示例,了解如何使用我的API密钥构造和发出调用并获得响应。
在伪代码中,我想要做的是:
String location = "123 main street, new york, ny";
String response = // send location plus my API key to google geocoder
// now have a json file response that I can deserialize?
发布于 2020-08-17 21:48:07
我建议您可以在GitHub上找到Google Maps团队的官方库Java Client for Google Maps Services,其中有一些使用它的基本示例。
在Readme.md文件中,您将找到如何通过Maven或Gradle将该库添加到您的项目中的说明。
通过地理编码API使用库的代码片段如下所示
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("YOUR_API_KEY")
.build();
String location = "123 main street, new york, ny";
try {
GeocodingApiRequest req = GeocodingApi.newRequest(context);
GeocodingResult[] results = req.address(address).await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResults = gson.toJson(results);
} catch(ApiException e){
//Handle API exceptions here
}
时区API的代码片段如下所示
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("YOUR_API_KEY")
.build();
LatLng location = new LatLng(41.385064,2.173403);
java.util.TimeZone result = TimeZoneApi.getTimeZone(context, location).await();
有关该库的类和方法的更多详细信息,请参阅JavaDoc,网址为
https://www.javadoc.io/doc/com.google.maps/google-maps-services/latest/index.html
享受吧!
https://stackoverflow.com/questions/63443104
复制相似问题