Pushpendra
Pushpendra

Reputation: 1521

Google Place api key is not working for android application

I am trying to fetch the Place with the help of Place API Keys. Same API keys is working fine for MAP but it's not working for Place Picker.

Here is code flow what I am doing....

  1. Enabled Maps SDK for Android
  2. Enabled Places API

Kye is restricted as mention into google document with the help of applicationId and SHA1.

Gradle file code app.gradle-

implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'

Now AndroidManifest.xml

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

These meta tag is inside Application TAG

Now here is my Activity code

btnLocation!!.setOnClickListener {
        val builder = PlacePicker.IntentBuilder()
        try {
            startActivityForResult(builder.build(this), placePickerRequestCode)
        }catch (e : GooglePlayServicesNotAvailableException){
            e.printStackTrace()
        }catch (e : GooglePlayServicesRepairableException){
            e.printStackTrace()
        }
    }

Resultant function onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == placePickerRequestCode) {
        if (resultCode == Activity.RESULT_OK) {
            val place = PlacePicker.getPlace(data!!, this)
            lat = place.latLng!!.latitude
            long = place.latLng!!.longitude
            tvLocation!!.text = place.name
        } else {
            Log.d(TAG, "ERROR_RES_CODE $resultCode")
            Log.d(TAG, "ERROR $data")
        }
    }
}

Here I am getting always ERROR_RES_CODE 2 and ERROR null

Please try to find the mistake what I am doing in that. I will be very thank full!

Upvotes: 0

Views: 1437

Answers (1)

Gavin Wright
Gavin Wright

Reputation: 3212

First off, you're not using the right dependency. As of 6 weeks ago, you would've wanted to use this one:

implementation 'com.google.android.gms:play-services-places:16.0.0'

But that version of the Places API has been deprecated. You should now be using this one:

implementation 'com.google.android.libraries.places:places:1.0.0'

Also, the PlacePicker is deprecated and will cease to work in June. So you really shouldn't even start using it:

https://developers.google.com/places/android-sdk/client-migration

If you want something similar to the map portion of the PlacePicker, it's pretty easy to replicate that with a map and the Geocoder class. If you want place autocomplete, you'll need to use one of these samples and you'll need to pay Google its ransom because there's no longer a free way to do that:

https://github.com/googlemaps/android-places-demos

Upvotes: 1

Related Questions