Android Get Current Place

Nov 26, 2019

Setup Android Places SDK.

Setup Request Location Permission.

class TestFragment: Fragment() {    private val placesClient by lazy {        Places.createClient(context!!)    }    fun findCurrentPlaces() {        val placeFields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.TYPES, Place.Field.LAT_LNG)        val request = FindCurrentPlaceRequest.newInstance(placeFields)        if (checkLocationPermission()) {            val placeResponse = placesClient.findCurrentPlace(request)            placeResponse.addOnCompleteListener { task ->                if (task.isSuccessful) {                    val response = task.result                    response?.placeLikelihoods?.forEach {                        Timber.d("id=${it.place.id}, name=${it.place.name}, score=${it.likelihood}")                    }                }                else {                    Timber.e(task.exception, "placesClient.findCurrentPlaces failed")                }            }        }        else {            requestLocationPermission()        }    }}

NOTE: Refer Request Location Permission for checkLocationPermission and requestLocationPermission implementation.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.

Tags