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: