androidgoogle-places-apionactivityresult

placesautocomplete result code = 2


I am trying to implement google Places APIs (Places Autocomplete) by creating an intent through intentBuilder. My app is successfully getting an intent with a button click but unable to search a place.

On overriding onActivityResult, it's getting resultCode = 2 instead of RESULT_SUCCESS, RESULT_ERROR or RESULT_CANCELLED and gets back to MainActivity.

I took reference from https://developers.google.com/places/android-api/autocomplete and added a dependency

compile 'com.google.android.gms:play-services-places:9.2.0' 

Here is my code

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

        } else if (resultCode == RESULT_CANCELED) {
            Log.i("message", "the user cancelled the operation" );            }
    }
}

I need some assistance, please help.


Solution

  • As the code is correct, it looks like there might be some error in your API_KEY. Please recheck the API_KEY, whether the correct SHA-1 fingerprint was used to generate the API_KEY, also don't forget to check whether you have enabled places API from the developer's console for the project. If all these are correct and you continue to face the same error, please send me the logs.

    Hope this helps. :)