5

I have been struggling with Google Places API, I need to use the Address API.

I used the autoComplete and the placePicker APIs just fine, for some reason the Address API is not working.

I have tried looking through this https://developers.google.com/android/reference/com/google/android/gms/identity/intents/Address
but I couldn't figure out how to use the addApi() for the Address API.

If someone could provide some example code or something to get me started it would be highly appreciated.

Thank you in advance.

Mazen Elian
  • 228
  • 1
  • 4
  • 19

1 Answers1

0

To add the Address API you need to add an option like this:

    Address.AddressOptions options = new  Address.AddressOptions(AddressConstants.Themes.THEME_LIGHT);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Address.API, options)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

Then you can request the address:

UserAddressRequest request = UserAddressRequest.newBuilder().build();
        Address.requestUserAddress(mGoogleApiClient, request,
                REQUEST_CODE);

And then you get the result:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    UserAddress userAddress = UserAddress.fromIntent(data);
                    //DO SOMETHING
                    break;
                case Activity.RESULT_CANCELED:
                    break;
                default:
                    //NO ADDRESS
                    break;
            }
            break;
    }
}

And add this to your gradle:

 compile 'com.google.android.gms:play-services-identity:8.1.0'
isma3l
  • 3,833
  • 1
  • 22
  • 28
  • Thank you very much for your reply, there is a however and issue. I need to implement the API code in a `FragmentActivity` so i can make a call to the `enableAutoManage()`. Also, implementing your code in an `Activity` returns "An Activity must be used for Address APIs" Exception. – Mazen Elian Oct 11 '15 at 21:34
  • 1
    @MazenElian you're right, i don't know why Address API doesn't like enableAutomanage(), in others API works fine, you can find a workaround in this answer http://stackoverflow.com/questions/30622906/using-enableautomanage-in-fragment – isma3l Oct 12 '15 at 15:37