Android Google Places Get Nearby Places (Google Play 15)

July 22, 2018

You need Places SDK for Android to get places data.

Setup Google Play Services

Edit build.gradle (Module)

dependencies {
    // https://developers.google.com/android/guides/releases
    // implementation "com.google.android.gms:play-services-location:15.0.1"
    implementation "com.google.android.gms:play-services-places:15.0.1"
}

NOTE: Set Up Google Play Services

Add your API key

NOTE: If you already setup Android Google Maps, this step is already done.

Edit AndroidManifest.xml

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

Refer to Get API Key and Signup or Get API Key.

NOTE: You can refer to Android Google Maps for some setup guidance as well.

Get Nearby Places

val placeDetectionClient = Places.getPlaceDetectionClient(context)

val task = placeDetectionClient.getCurrentPlace(null)

// val result = Tasks.await(placeResult)
placeResult.addOnCompleteListener { task ->
    val result = task.result

    for (placeLikelihood in result) {
        val place = placeLikelihood.place

        // place.id, place.name, place.address, place.placeTypes, place.latLng
    }

    result.release()
}

Refer to Current Place.

NOTE: Sadly there is no way to find nearby places by specifying a specific location. You can search at particular location using autocomplete, but a query string is required.

Display attributions

To use google places without a Map, you need display attributions.

To display “Powered by Google” drawable in TextView

val attributionText = SpannableStringBuilder(" ").apply {
    setSpan(ImageSpan(context, R.drawable.powered_by_google_dark), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
textView.text = attributionText

You need to display third-party attributions as well.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.