Migrating to the GoogleApi Client

The 11.2.0 release of the Google Play services SDK includes a new way to access the Places SDK for Android. The GoogleApi client is easier to use than its predecessor (GoogleApiClient), since it automatically manages connections to Google Play services. This reduces the amount of boilerplate code in your app, and can help to eliminate many common pitfalls. The new API offers a number of improvements:

  • The connection process is managed automatically, so the new API is less work to implement.
  • API calls now automatically wait for the service connection to be established, removing the need to wait for onConnected before making requests.
  • The Tasks API makes it easier to compose asynchronous operations.
  • The code is self-contained, and can easily be moved into a shared utility class or similar.

Updating your app to use the GoogleApi client requires some changes to your Places SDK for Android implementation. This guide describes the changes to the Places SDK for Android, and recommends steps to take when updating your app to use the new client.

Overview

The main areas of change are as follows:

  • There are two new entry points: GeoDataClient and PlaceDetectionClient. Instead of creating one GoogleApiClient instance to cover all APIs, your app must now instantiate both GeoDataClient and PlaceDetectionClient.
  • Since connection callbacks are no longer required, you can safely refactor your app to remove them.
  • The new Places API methods are now asynchronous, and return a Task rather than a PendingResult.

Load the Places API

To load the Places API, declare the entry points, then instantiate the clients in your fragment's or activity's onCreate() method as shown in the following example:

// The entry points to the Places API.
private GeoDataClient mGeoDataClient;
private PlaceDetectionClient mPlaceDetectionClient;

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

Comparison

The new Places API methods are now asynchronous, and return a Task rather than a PendingResult. The data structures have not changed, so your existing code for handling results shouldn't need updating. The following code examples compare the new and old versions of GetCurrentPlace():

The new way

Task<PlaceLikelihoodBufferResponse> placeResult = mPlaceDetectionClient.getCurrentPlace(null);
placeResult.addOnCompleteListener(new OnCompleteListener<PlaceLikelihoodBufferResponse>() {
    @Override
    public void onComplete(@NonNull Task<PlaceLikelihoodBufferResponse> task) {
        PlaceLikelihoodBufferResponse likelyPlaces = task.getResult();
        for (PlaceLikelihood placeLikelihood : likelyPlaces) {
            Log.i(TAG, String.format("Place '%s' has likelihood: %g",
                placeLikelihood.getPlace().getName(),
                placeLikelihood.getLikelihood()));
        }
        likelyPlaces.release();
    }
});

The old way

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
    .getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
  @Override
  public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
    for (PlaceLikelihood placeLikelihood : likelyPlaces) {
      Log.i(TAG, String.format("Place '%s' has likelihood: %g",
          placeLikelihood.getPlace().getName(),
          placeLikelihood.getLikelihood()));
    }
    likelyPlaces.release();
  }
});

Learn more

Learn more about accessing Google APIs.