androidkotlingoogle-places-apigoogle-places

How to implement google places autocomplete programmatically and get also the latlng with kotlin?


I get the address but not the latlng

package shweta.com.googleplacesautocomplete

import android.util.Log
import android.webkit.WebStorage
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.tasks.Tasks
import com.google.android.libraries.places.api.model.AutocompletePrediction
import com.google.android.libraries.places.api.model.AutocompleteSessionToken
import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.api.model.TypeFilter
import com.google.android.libraries.places.api.net.FetchPlaceRequest
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest
import com.google.android.libraries.places.api.net.PlacesClient
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

const val TASK_AWAIT = 120L
const val MAP_CAMERA_ZOOM = 11f
const val MAP_CAMERA_ZOOM_INT = 11

/**
 * This fun is used to move map
 * @receiver GoogleMap
 * @param latLng LatLng?
 */
fun GoogleMap.moveCameraOnMap(latLng: LatLng?) {
    this.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, MAP_CAMERA_ZOOM))
}

/**
 * This fun is used to move map
 * @receiver GoogleMap
 * @param latLng LatLng?
 */
fun GoogleMap.moveCameraOnMapBound(latLng: LatLngBounds?) {
    this.animateCamera(CameraUpdateFactory.newLatLngBounds(latLng, MAP_CAMERA_ZOOM_INT))
}

/**
 * This fun is used to get auto complete fields
 * @param mGeoDataClient GeoDataClient
 * @param constraint CharSequence
 * @return ArrayList<AutocompletePrediction>?
 */
fun getAutocomplete(mPlacesClient: PlacesClient, constraint: CharSequence): List<AutocompletePrediction> {
    var list = listOf<AutocompletePrediction>()
    val token = AutocompleteSessionToken.newInstance()

    val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.ADDRESS_COMPONENTS, Place.Field.TYPES)

    val request = FindAutocompletePredictionsRequest
        .builder()
        .setTypeFilter(TypeFilter.ADDRESS)
        .setCountry("CL")
        .setSessionToken(token)
        .setQuery(constraint.toString())
        .build()

    val prediction = mPlacesClient.findAutocompletePredictions(request)
    
    try {
        Tasks.await(prediction, TASK_AWAIT, TimeUnit.SECONDS)
    } catch (e: ExecutionException) {
        e.printStackTrace()
    } catch (e: InterruptedException) {
        e.printStackTrace()
    } catch (e: TimeoutException) {
        e.printStackTrace()
    }

    if (prediction.isSuccessful) {
        val findAutocompletePredictionsResponse = prediction.result
        findAutocompletePredictionsResponse?.let {
            list = findAutocompletePredictionsResponse.autocompletePredictions
        }
        return list
    }
    return list
}

code based on How to implement google places autocomplete programmatically

https://github.com/ShwetaChauhan18/GooglePlacesAutoComplete

I tried following the tutorial at the following link: https://www.youtube.com/watch?v=6Trdd9EnmqY&list=PLgCYzUzKIBE-vInwQhGSdnbyJ62nixHCt&index=8. Some methods used in it recently became deprecated, so I checked the documentation at https://developers.google.com/places/android-sdk/autocomplete, especially "Get place predictions programmatically" and "Migrating to the New Places SDK Client". But I have not been able to get all the places in kotli


Solution

  • Pass the selected prediction to query more details as shown below.

    fun fetchPlace(prediction: AutocompletePrediction) {
    
        // Specify the fields to return.
        val placeFields = listOf(Place.Field.ID, Place.Field.ADDRESS, Place.Field.LAT_LNG)
        val request = FetchPlaceRequest.newInstance(prediction.placeId, placeFields)
    
        // Use same PlacesClient instance used to fetch predictions
        placesClient.fetchPlace(request).addOnSuccessListener { response: FetchPlaceResponse ->
            response.place.latLng?.let {
                // lat lng details
            }
        }.addOnFailureListener { exception: Exception ->
            if (exception is ApiException) {
                val statusCode = exception.statusCode
            }
        }
    }