Google places API, get user addresses

空扰寡人 提交于 2019-12-01 17:29:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!