问题
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.
回答1:
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'
来源:https://stackoverflow.com/questions/32912781/google-places-api-get-user-addresses