← Back to list

Google Places API with custom UI for the suggestions.

As you all have read in the Google document about the Places API, we have three methods available to implement Google Places.

Kardamshubhamkumar · 2023-08-16 12:37 · 9 claps · 2.4 min read
#google-place-autocomplete #google-places-api #google-places #google-place-optimizaiton
Open on Medium ↗

Google Places API with custom UI for the suggestions.

As you all have read in the Google document about the Places API, we have three methods available to implement Google Places.

Option 1: Embedded an autocomplete fragment

Option 2: Use and intent to launch an autocomplete activity.

Option 3: Getting place predictions programmatically.

Next, we will explore option 3, which involves creating a personalized user interface that displays location predictions through programming.

Let's create a magic recipe. Here we go

Firstly, we need to include the dependency in Gradle.

implementation("com.google.android.libraries.places:places:3.1.0")

i) Initialize the Places API client.

// Initialize the SDK
Places.initialize(applicationContext, apiKey)

// Create a new PlacesClient instance
val placesClient = Places.createClient(this)

To access the place's APIs, initialize the Places API in the Application class or the base Application class and create the Place client.

ii) initialize the recycler view and adapter

@Inject lateinit var placePredictionAdapter: PlacePredictionAdapter

inject the PlacePredictionAdapter using the hilt.

with(binding.placeSuggestionRecyclerView){
    layoutManager = LinearLayoutManager(requireContext())
    adapter = placePredictionAdapter
    placePredictionAdapter.callbackPlaceSelected {
        // here you got selected place 
        val placeId = it.placeId
        this.visibility = View.GONE
    }
}

setup recycler view and set the recycler view adapter with placePredictionAdapter.

Here, you have got the place ID and pass this place id into the fetch place API ( refer to point 6)

iii ) Use [findAutocompletePredictions()](https://developers.google.com/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/api/net/PlacesClient#findAutocompletePredictions(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)) to return place predictions in response to user search queries.

The following example shows calling findAutocompletePredictions()

// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
// and once again when the user makes a selection (for example when calling fetchPlace()).
val token: AutocompleteSessionToken = AutocompleteSessionToken.newInstance()
// Create a RectangularBounds object.
 val bounds: RectangularBounds = RectangularBounds.newInstance(
  new LatLng(-33.880490, 151.184363),
  new LatLng(-33.858754, 151.229596));
// Use the builder to create a FindAutocompletePredictionsRequest.
val request: FindAutocompletePredictionsRequest = FindAutocompletePredictionsRequest.builder()
// Call either setLocationBias() OR setLocationRestriction().
   .setLocationBias(bounds)
   //.setLocationRestriction(bounds)
   .setCountry("au")
// here can add multiple place type but there are some condition for more look into the doc 
// like .setTypesFilter(listOf( "administrative_area_level_1", "premise", "colloquial_area", "landmark", "street_number"))
   .setTypesFilter(Arrays.asList(PlaceTypes.ADDRESS))
   .setSessionToken(token)
    // here query is String type that reperest the place that want to serach
   .setQuery(query)
   .build()

placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
    val placeSuggestions = response. autocompletePredictions
    Log.d("list of suggestions", "${placeSuggestions}")
    // here pass this list to adapter to show the suggestions
    placePredictionAdapter.setPredictions(placesSuggestion)

}).addOnFailureListener((exception) -> {
   if (exception is ApiException) {
       ApiException apiException = (ApiException) exception
       Log.d(TAG, "Place not found: " + apiException.getStatusCode())
   }
})

bound: you can create a boundary to get the suggestion of a list inside this boundary.

By setting country, restrict the suggestion when the user enters a search query.

pass the “search query ” in the set query

for setting more type filters look into this. https://developers.google.com/maps/documentation/places/android-sdk/supported_types

iv) When the user clicks on any suggestion then pass the place ID into the fetchPlace API

// add place field that you need of
val placeFields: List<Place.Field> =
            listOf(Place.Field.ID, 
                  Place.Field.NAME, 
                  Place.Field.ADDRESS_COMPONENTS, 
                  Place.Field.LAT_LNG, 
                  Place.Field.PLUS_CODE,
                  Place.Field.ADDRESS)
// create a request with place id and place fields
val request = FetchPlaceRequest.builder(placeId, placeFields)
    .build()

// add place result listener
placesClient.fetchPlace(request).addOnSuccessListener { response ->
    val place: Place = response.place
    val name: String = place.name
    val address: String = place.address
    Log.d("TAG", "Place found: " + place.name)
}.addOnFailureListener { exception ->
    Log.d("TAG", "Place not found: " + exception)
}

After following all steps you’ll see the custom nice view that is showing suggestions for the place

References:

[embed]Place Autocomplete | Places SDK for Android | Google for Developers The autocomplete service in the Places SDK for Android returns place predictions in response to user search queries. As…developers.google.com


메타데이터
post_id
81fc07a6ffd
slug
google-places-api-with-custom-ui-for-the-suggestions-81fc07a6ffd
url
https://medium.com/@kardamshubhamkumar/google-places-api-with-custom-ui-for-the-suggestions-81fc07a6ffd
canonical_url
https://medium.com/@kardamshubhamkumar/google-places-api-with-custom-ui-for-the-suggestions-81fc07a6ffd
author_url
https://medium.com/@kardamshubhamkumar
status
ok
fetched_at
2026-08-10 12:41:46