Reputation: 31
I am implementing google places api in my android app. I open the google auto complete textview, and i try to type. Once I started typing the fragment closes automatically. I used both the intent and listener but both are giving the same problem. here is the code:
Code for Listener:
autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteSupportFragment.setTypeFilter(TypeFilter.CITIES);
autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
Toast.makeText(getContext(), ""+place.getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull Status status) {
}
});
Code for Intent
lc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.build(getContext());
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getContext(), ""+place.getName(), Toast.LENGTH_SHORT).show();
} 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.
}
}
}
Code for layout:
<fragment
android:id="@+id/place_autocomplete_fragment1"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp" />
</LinearLayout>
I saw other solutions but it was not helpfull. What changes should i have. I have enabled google places in the console as well.
Upvotes: 3
Views: 3778
Reputation: 11
There's an additional configuration that you have to add in the google maps API
Upvotes: 1
Reputation: 59
I don't have a solution, however, I may provide a way to help troubleshoot this issue.
In the onActivityResult
method put this Log :
Status status = Autocomplete.getStatusFromIntent(data);
Log.e("SomeTagToFilterTheLogcat", status.toString());
And then see the output to determine the error. Mine was that billing was not enabled though my google console account says otherwise, maybe because I am using the free tier with an old account where GCP was not requiring any credit cards.
Upvotes: 3
Reputation: 5644
I had the same problem. You should initialize like this:
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "Your_Api_Key");
}
PlacesClient placesClient = Places.createClient(this);
Upvotes: 0
Reputation: 5463
This happens when the project is not authorized to use API key you are trying to use.
try using the link from the google_maps_api.xml
file which is generated for you automatically when you use the wizard.
It has the format
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=SHA1KEY;packagename&pli=1
example: if your package is com.example.myapplication
and your SHA-1 key is 61:DB:7B:CB:A9:86:62:51:CF:88:9E:AC:ED:PC:E8:2E:CB:C6:43:60
Upvotes: 2
Reputation: 435
you should log your status in onError method to know the problem
Upvotes: 0