To configure your app to use the Google Places API for Android follow these procedures.
Configure your app
The following configuration steps are required for all apps using the Google Places API for Android.
1. Set up Google Play services
To access the Google Places API for Android, your app's development project must include Google Play services. Download and install the Google Play services component via the SDK Manager and add the library to your project. For details, see the Android guide to setting up Google Play services.
2. Add your API key
If you haven't already obtained an API key and enabled the Google Places API for Android, follow the instructions in the signup guide to get one now.
Add your API key to your app manifest as shown in the following code sample, replacing YOUR_API_KEY with your own API key:
<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>
3. Request an uplift in usage limits
Usage of the Google Places API for Android is free. However, to ensure fair use by all apps, there are tiered query limits on some methods.
The Google Places API for Android enforces a default limit of 1,000 requests per 24 hour period. If your app exceeds the limit, the app will start failing.
There is a further checkpoint when the app reaches 150,000 requests per 24 hour period. If your app exceeds the limit, the app will start failing again.
Please see the usage limits guide for more information.
Instantiate the Places API clients
Google Play services version 11.2 includes a new way to access the Places APIs. Now, instead of connecting to Google Play services, all your app must do is instantiate the desired interfaces. It's no longer necessary to manage connections.
The following interfaces provide the primary entry points to the Google Places API for Android:
- The
GeoDataClientprovides access to Google's database of local place and business information. - The
PlaceDetectionClientprovides quick access to the device's current place, and offers the opportunity to report the location of the device at a particular place.
To use the APIs, instantiate one or both interfaces in your
fragment's or activity's
onCreate()
method, as shown in the following code sample:
import com.google.android.gms.location.places.GeoDataClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.PlaceDetectionClient;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
protected GeoDataClient mGeoDataClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Construct a GeoDataClient.
mGeoDataClient = Places.getGeoDataClient(this, null);
// Construct a PlaceDetectionClient.
mPlaceDetectionClient = Places.getPlaceDetectionClient(this, null);
// TODO: Start using the Places API.
}
}
Connect to Google Play services 11.0 or earlier
Apps using Google Play services version 11.0 or earlier must connect to the Google Play services API client. The following interfaces provide the primary entry points to the Google Places API for Android:
- The
GeoDataApiprovides access to Google's database of local place and business information. - The
PlaceDetectionApiprovides quick access to the device's current place, and offers the opportunity to report the location of the device at a particular place.
To connect to the APIs, you need to create an instance of the Google Play
services API client. In your fragment's or activity's
onCreate()
method, create an instance of Google API Client using
GoogleApiClient.Builder.
Use the builder to add the required APIs, as shown in the following code sample:
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import android.support.v4.app.FragmentActivity;
public class MyActivity extends FragmentActivity
implements OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
}
// TODO: Please implement GoogleApiClient.OnConnectionFailedListener to
// handle connection failures.
}
As shown in the code sample, the
Places
class provides tokens that you can pass to
GoogleApiClient.Builder
to enable the
GeoDataApi
and
PlaceDetectionApi.
For details about using the client, see the Android guide to
Accessing
Google APIs. In particular, see the guides to
starting
a connection and
handling
connection failures, for more information about
addConnectionCallbacks() and
addOnConnectionFailedListener().