- Notifications
You must be signed in to change notification settings - Fork961
Java client library for Google Maps API Web Services
License
googlemaps/google-maps-services-java
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Tip
If you are looking for Java client libraries for the following APIs, see theGoogle Maps Platform APIs in the Cloud Client Libraries for Java (releases).
The new APIs will not be added to this client library.
Use Java? Want togeocode something? Looking fordirections?Maybematrices of directions? This library brings theGoogle Maps API WebServices to your server-side Java application.
The Java Client for Google Maps Services is a Java Client library for the following Google MapsAPIs:
- Directions API
- Distance Matrix API
- Elevation API
- Geocoding API
- Maps Static API
- Places API
- Roads API
- Time Zone API
- Java 1.8 or later
- ACloud project with the relevant API(s) enabled
- AnAPI key associated with the project above
The Java Client for Google Maps Services is designed for use in both server and Android applications.In either case, it is important to addAPI key restrictionsto improve the security of your API key. Additional security measures, such as hiding your keyfrom version control, should also be put in place to further improve the security of your API key.
You can refer toAPI Security Best Practices to learnmore about this topic.
Note
If you are using this library on Android, ensure that your application is using at least version 0.19.0 of this library so that API key restrictions can be enforced.
You can add the library to your project via Maven or Gradle.
Note
Since 0.1.18 there is now a dependency onSLF4J. You need to add one of the adapter dependencies that makes sense for your logging setup. In the configuration samples below we are integratingslf4j-nop, but there are others likeslf4j-log4j12 andslf4j-jdk14 that will make more sense in other configurations. This will stop a warning message being emitted when you start usinggoogle-maps-services.
<dependency> <groupId>com.google.maps</groupId> <artifactId>google-maps-services</artifactId> <version>(insert latest version)</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version></dependency>
repositories { mavenCentral()}dependencies { implementation'com.google.maps:google-maps-services:(insert latest version)' implementation'org.slf4j:slf4j-simple:1.7.25'}You can find the latest version at the top of this README or bysearchingMaven Central.
View thejavadoc.
Additional documentation for the included web services is available athttps://developers.google.com/maps/.
- Directions API
- Distance Matrix API
- Elevation API
- Geocoding API
- Maps Static API
- Places API
- Roads API
- Time Zone API
This example uses theGeocoding API with an API key:
GeoApiContextcontext =newGeoApiContext.Builder() .apiKey("AIza...") .build();GeocodingResponseresponse =GeocodingApi.geocode(context,"1600 Amphitheatre Parkway Mountain View, CA 94043").await();Gsongson =newGsonBuilder().setPrettyPrinting().create();System.out.println(gson.toJson(response.results[0].addressComponents));// Invoke .shutdown() after your application is done making requestscontext.shutdown();
Note: TheGeoApiContext is designed to be aSingletonin your application. Please instantiate one on application startup, and continue to use it for thelife of your application. This will enable proper QPS enforcement across all of your requests.
At the end of the execution, call theshutdown() method ofGeoApiContext,otherwise the thread will remain instantiated in memory.
For more usage examples, check outthe tests.
To use Google App Engine with this client library add the latestApp Engine dependencyto yourbuild.gradle file:
dependencies { implementation'com.google.appengine:appengine-api-1.0-sdk:<latest version>'}You can then use this client library on Google App Engine with the following code change:
newGeoApiContext.Builder(newGaeRequestHandler.Builder()) .apiKey("AIza...") .build();
Thenew GaeRequestHandler.Builder() argument toGeoApiContext.Builder'srequestHandlerBuildertells the Java Client for Google Maps Services to utilise the appropriate calls for making HTTPrequests from Google App Engine, instead of the defaultOkHttp3based strategy.
Never sleep between requests again! By default, requests are sent at the expected rate limits foreach web service, typically 50 queries per second for free users. If you want to speed up or slowdown requests, you can do that too, usingnew GeoApiContext.Builder().queryRateLimit(qps).build().Note that you still need to manually handle thedelay between the initial request and successive pages when you're paging through multiple result sets.
Automatically retry when intermittent failures occur. That is, when any of the retriable 5xx errorsare returned from the API.
To alter or disable automatic retries, see these methods inGeoApiContext:
.disableRetries().maxRetries().retryTimeout().setIfExceptionIsAllowedToRetry()
Native objects for each of the API responses.
All requests support synchronous or asynchronous calling style.
GeocodingApiRequestreq =GeocodingApi.newRequest(context).address("Sydney");// Synchronoustry {req.await();// Handle successful request.}catch (Exceptione) {// Handle error}req.awaitIgnoreError();// No checked exception.// Asyncreq.setCallback(newPendingResult.Callback<GeocodingResponse>() {@OverridepublicvoidonResult(GeocodingResponseresult) {// Handle successful request. }@OverridepublicvoidonFailure(Throwablee) {// Handle error. }});
Note
You will need an API key to run the tests.
# Compile and package the project$ ./gradlew jar# Run the tests$ ./gradlewtest
This library uses Google Maps Platform services. Use of Google Maps Platform services through this library is subject to theGoogle Maps Platform Terms of Service.
This library is not a Google Maps Platform Core Service. Therefore, the Google Maps Platform Terms of Service (e.g. Technical Support Services, Service Level Agreements, and Deprecation Policy) do not apply to the code in this library.
This library is offered via an open source license. It is not governed by theGoogle Maps Platform Support Technical Support Services Guidelines, theSLA, or theDeprecation Policy (however, any Google Maps Platform services used by the library remain subject to the Google Maps Platform Terms of Service).
This library adheres tosemantic versioning to indicate when backwards-incompatible changes are introduced. Accordingly, while the library is in version 0.x, backwards-incompatible changes may be introduced at any time.
If you find a bug, or have a feature request, please file anissue on GitHub. If you would like to get answers to technical questions from other Google Maps Platform developers, ask through one of ourdeveloper community channels. If you'd like to contribute, please check theContributing guide in the GitHub repository.
About
Java client library for Google Maps API Web Services
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.