CHarris
CHarris

Reputation: 2803

How do I get the full address of location using Google Places Autocomplete?

I am using Google Places Autocomplete. They start typing and when a user clicks on an address in the autocomplete list, I want this address to show in my EditText.

For example, when I click on 22 Moyclare Road, Dublin 13, Ireland in the image below, I want 22 Moyclare Road, Dublin 13, Ireland to show in my Edittext.

<code>enter image description here</code>

But all I am getting is Moyclare Road, like this:

enter image description here

I am using the getName property of place like: mEditInit.setText(place.getName()); but no matter what property I use I can't get it to display full address. Any ideas?

Here is the entire code of my onActivityResult function:

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {

        Place place = Autocomplete.getPlaceFromIntent(data);

        mEditInit.setText(place.getName());
      } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        // TODO: Handle the error.
        Status status = Autocomplete.getStatusFromIntent(data);
        // Log.i(TAG, status.getStatusMessage());
      } else if (resultCode == RESULT_CANCELED) {
        // The user canceled the operation.
      }
      //  super.onActivityResult(requestCode, resultCode, data);
    }
  }

Upvotes: 3

Views: 1513

Answers (1)

evan
evan

Reputation: 5699

You need to use the place.getAddress() method instead of place.getName() to get the full address of a place.

mEditInit.setText(place.getAddress());

Make sure that you specify the address field, i.e. Place.Field.ADDRESS. Refer to Google's documentation.

To get the full address for the Irish place you're searching for, instead of selecting 22 Moyclare Road, Dublin 13, Ireland, select the Northside result from Autocomplete, i.e. 22 Moyclare Road, Northside, Dublin 13, Ireland.

You'll get the following as output from getAddress:

22 Moyclare Rd, Northside, Dublin 13, D13 W2X2, Ireland

As for how to show this in the search bar, check out the accepted answer for related question placeautocompletefragment full address

Hope this helps!

Upvotes: 1

Related Questions