I am using Google Place to get place details. Google provide different way to implement Google Place API to get place details.Different way are like PlaceAutocompleteFragment , PlaceAutocompleteActivity . How differentiate these all and how to implement to get place details using Google place API.
Asked
Active
Viewed 3.7k times
1 Answers
38
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:
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:
Hope its help.
Edit: change requestCode == RESULT_CANCELED
to resultCode == RESULT_CANCELED

florian-do
- 865
- 1
- 13
- 27

pRaNaY
- 24,642
- 24
- 96
- 146
-
how will you select from the autocomplete list and show it on map – Srishti Roy Jan 27 '16 at 07:17
-
@SrishtiRoy . Need to try [PlaceCompleteAdapter](https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter) – pRaNaY Jan 27 '16 at 09:56
-
4@pRaNaY how to customize the appearance of PlaceAutoCompleteFragment with the nearby Views in layout – Tejzeratul Aug 24 '16 at 06:50
-
1`getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);` returns null... – Raphael Royer-Rivard Sep 12 '16 at 01:35
-
1Would you mind posting your imports? I have a compilation error on 'autocompleteFragment.setOnPlaceSelectedListener(...)'. After the assignment of autocompleteFragment via 'PlaceAutocompleteFragment autocompleteFragment = ...' the only options that popup after entering the "autocompleteFragment." are "instantiationException" and "SavedState". I assume there's something wrong with my imports, but I can't figure out what it is. – John Ward Oct 18 '16 at 00:39
-
@JohnWard: Check my example repo [MainActivity](https://github.com/pranaypatel512/PlaceAutocomplete/blob/android/app/src/main/java/com/placeautocompletefragmentDemo/app/MainActivity.java) for same example,which helps you to resolve your conflicts. – pRaNaY Oct 18 '16 at 02:49
-
@RaphaelRoyer-Rivard : you need to add fragment in your XML : see my example XML [here](https://github.com/pranaypatel512/PlaceAutocomplete/blob/android/app/src/main/res/layout/content_main.xml) – pRaNaY Oct 18 '16 at 02:51
-
Works flawlessly for me, thank you kindly! Also, if you guys just want the looks of the autocomplete fragment but don't want to struggle implementing it, just change `Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this);` to `Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).build(this);`. Maybe it is a little obvious but I struggled for a while. – McSullivan Nov 16 '16 at 21:38
-
8Don't forget to add this: `compile 'com.google.android.gms:play-services-places:9.4.0' ` – Rafael Dec 07 '16 at 05:06
-
1I have done the same. But when I write some place to search, it disappears. What would be the problem? – Waqas Ahmed Ansari Jan 07 '17 at 06:22
-
1@WaqasAhmedAnsari : make sure you enable google place API in your API console and your API key properly configure with your SHA1. You get proper guide to configure place API here: https://developers.google.com/places/android-api/signup – pRaNaY Jan 07 '17 at 06:29
-
I've done all. I've implemented some functions on map too, like adding marker and line. The problem is only with search – Waqas Ahmed Ansari Jan 07 '17 at 06:48
-
Don't forget to enable Places API i Google Console or you will face an issue, of loosing focus of the PlaceAutocomplete, when clicking it. Check out Logcat for more info. You can use the onError callback. – Reejesh PK Oct 27 '18 at 04:16