How to Implement Placeautocompletefragment and Placeautocompleteactivity to Get Place Details

How to Implement PlaceAutocompleteFragment and PlaceAutocompleteActivity to get Place details

First of all need to API key and Enable Google Place API to search and get place details.
Add your API key to your app manifest ,need to replacing YOUR_API_KEY with your own API key:

<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>

1) PlaceAutocompleteFragment

Xml:

<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>

Java:

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

/*
* The following code example shows setting an AutocompleteFilter on a PlaceAutocompleteFragment to
* set a filter returning only results with a precise address.
*/
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
autocompleteFragment.setFilter(typeFilter);

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName());//get place details here
}

@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});

Output:

Sample Image

2) PlaceAutocompleteActivity

private void callPlaceAutocompleteActivityIntent() {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
//PLACE_AUTOCOMPLETE_REQUEST_CODE is integer for request code
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//autocompleteFragment.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.i(TAG, "Place:" + place.toString());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {

}
}
}

Output:

Sample Image

Hope its help.

Edit: change requestCode == RESULT_CANCELED to resultCode == RESULT_CANCELED

Android using PlaceAutoCompleteActivity (Google Places API)

You may need to insert your own API key (if you haven't done so already) that can be acquired through the Google developers console.
Details here:
https://developers.google.com/places/android-api/signup?hl=en
Also, in the Google developers console, add Google Places API to the list of enabled API's in your project.

Adding PlaceAutocompleteFragment to an activity throws error

cannot resolve symbol PlaceAutocompleteFragment.

The compiler cannot find the class. This means that the class has not been imported properly, the name is misspelt or the class simply does not exist.

Just add this

compile 'com.google.android.gms:play-services-location:8.4.0'

Finally

dependencies {
compile 'com.google.android.gms:play-services-location:8.4.0'
}

You can visit How to Implement PlaceAutocompleteFragment and PlaceAutocompleteActivity to get Place details

Places auto complete is opening a new overlay google search

According to the Place Autocomplete documentation here, you can have either Autocomplete widget in overlay mode (this is called "MODE_OVERLAY"), or you can have Autocomplete widget in fullscreen mode (this is called MODE_FULLSCREEN ).

When I first got to know place autocomplete I encountered this problem as well but it seems like you can't achieve interaction with your pressed view and you can only choose between MODE_FULLSCREEN and MODE_OVERLAY as I mentioned above(all according to the documentation)

Google Map places autocomplete doesn't work

I got the same error "error 13" ,and I asked that question in stackoverflow .

Although I did not get any answer but I got a comment from the person who works at Google maps. He said its a bug from googles side

Here is the link to my question

I looked for answers try them :

api key library should consist places api for android,web and maps
multidex support should be there



Related Topics



Leave a reply



Submit