Coder Social home page Coder Social logo

google-maps-services-java's Introduction

Java Client for Google Maps Services

Build Status Maven Central Version Coverage Status

Description

Use Java? Want to geocode something? Looking for directions? Maybe matrices of directions? This library brings the Google Maps API Web Services to your server-side Java application. Analytics

The Java Client for Google Maps Services is a Java Client library for the following Google Maps APIs:

Keep in mind that the same terms and conditions apply to usage of the APIs when they're accessed through this library.

Note: The Java Client for Google Maps Services is for use in server applications. If you're building a mobile application, you will need to introduce a proxy server to act as intermediary between your mobile application and the Google Maps API Web Services. The Java Client for Google Maps Services would make an excellent choice as the basis for such a proxy server.

Support

This library is community supported. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will try to support, through Stack Overflow, the public and protected surface of the library and maintain backwards compatibility in the future; however, while the library is in version 0.x, we reserve the right to make backwards-incompatible changes. If we do remove some functionality (typically because better functionality exists or if the feature proved infeasible), our intention is to deprecate and give developers a year to update their code.

If you find a bug, or have a feature suggestion, please log an issue. If you'd like to contribute, please read How to Contribute.

Requirements

  • Java 1.7 or later.
  • A Google Maps API key.

API keys

Each Google Maps Web Service requires an API key or Client ID. API keys are freely available with a Google Account at https://developers.google.com/console. To generate a server key for your project:

  1. Visit https://developers.google.com/console and log in with a Google Account.
  2. Select an existing project, or create a new project.
  3. Click Enable an API.
  4. Browse for the API, and set its status to "On". The Java Client for Google Maps Services accesses the following APIs:
    • Directions API
    • Distance Matrix API
    • Elevation API
    • Geocoding API
    • Places API
    • Roads API
    • Time Zone API
  5. Once you've enabled the APIs, click Credentials from the left navigation of the Developer Console.
  6. In the "Public API access", click Create new Key.
  7. Choose Server Key.
  8. If you'd like to restrict requests to a specific IP address, do so now.
  9. Click Create.

Your API key should be 40 characters long, and begin with AIza.

Important: This key should be kept secret on your server.

Installation

You can add the library to your project via Maven or Gradle.

Maven

<dependency>
    <groupId>com.google.maps</groupId>
    <artifactId>google-maps-services</artifactId>
    <version>(insert latest version)</version>
</dependency>

Gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.maps:google-maps-services:(insert latest version)'
    ...
}

You can find the latest version at the top of this README or by searching Maven Central or Gradle, Please.

Developer Documentation

View the javadoc.

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

Usage

This example uses the Geocoding API.

GeoApiContext context = new GeoApiContext().setApiKey("AIza...");
GeocodingResult[] results =  GeocodingApi.geocode(context,
    "1600 Amphitheatre Parkway Mountain View, CA 94043").await();
System.out.println(results[0].formattedAddress);

For more usage examples, check out the tests.

Features

Google App Engine Support

You can use this client library on Google App Engine with a single line code change.

GeoApiContext context = new GeoApiContext(new GaeRequestHandler()).setApiKey(API_KEY);

The new GaeRequestHandler() argument to the GeoApiContext constructor tells the Java Client for Google Maps Services to utilise the apropriate calls for making HTTP requests from Google App Engine, instead of the default OkHttp based strategy.

Rate Limiting

Never sleep between requests again! By default, requests are sent at the expected rate limits for each web service, typically 10 queries per second for free users. If you want to speed up or slow down requests, you can do that too, using new GeoApiContext().setQueryRateLimit(qps).

Retry on Failure

Automatically retry when intermittent failures occur. That is, when any of the retriable 5xx errors are returned from the API.

Keys and Client IDs

Maps API for Work customers can use their client ID and secret to authenticate. Free customers can use their API key, too.

POJOs

Native objects for each of the API responses.

Asynchronous or synchronous -- you choose

All requests support synchronous or asynchronous calling style.

GeocodingApiRequest req = GeocodingApi.newRequest(context).address("Sydney");

// Synchronous
try {
    req.await();
    // Handle successful request.
} catch (Exception e) {
    // Handle error
}

req.awaitIgnoreError(); // No checked exception.

// Async
req.setCallback(new PendingResult.Callback<GeocodingResult[]>() {
  @Override
  public void onResult(GeocodingResult[] result) {
    // Handle successful request.
  }

  @Override
  public void onFailure(Throwable e) {
    // Handle error.
  }
});

Building the Project

Note: You will need an API key or Client ID to run the tests.

# Compile and package the project
$ ./gradlew jar

# Run the tests
$ API_KEY=AIza.... ./gradlew test

# Run the tests with enterprise credentials.
$ CLIENT_ID=... CLIENT_SECRET=... ./gradlew test

# Generate documentation
$ ./gradlew javadoc

# Publish documentation
$ ./gradlew javadoc
$ git checkout gh-pages
$ rm -rf javadoc
$ mkdir $VERSION
$ mv build/docs/javadoc $VERSION
$ git add $VERSION/javadoc
$ git add latest
$ git commit
$ git push origin gh-pages

google-maps-services-java's People

Contributors

markmcd avatar domesticmouse avatar broady avatar dh-- avatar samthor avatar mwindsor-beoped avatar nicolaspoirier avatar nutsiepully avatar rsertelon avatar vassilijakovlev avatar gitbrantley avatar bamnet avatar mikkelspring avatar

Watchers

James Cloos avatar Hosea Sandstrom avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.