Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java client library for Google Maps API Web Services

License

NotificationsYou must be signed in to change notification settings

googlemaps/google-maps-services-java

Build StatusCoverage StatusMaven Central VersionJavadocsGitHub contributorsStack Exchange questions

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.

Description

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:

Requirements

  • Java 1.8 or later
  • ACloud project with the relevant API(s) enabled
  • AnAPI key associated with the project above

API Key Security

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.

Installation

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.

Maven

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

Gradle

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.

Documentation

View thejavadoc.

Additional documentation for the included web services is available athttps://developers.google.com/maps/.

Usage

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.

Features

Google App Engine Support

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.

Rate Limiting

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.

Retry on Failure

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

POJOs

Native objects for each of the API responses.

Asynchronous or synchronous -- you choose

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

Building the Project

Note

You will need an API key to run the tests.

# Compile and package the project$ ./gradlew jar# Run the tests$ ./gradlewtest

Terms

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.

Support

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

Stars

Watchers

Forks

Packages

 
 
 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp