How to implement suggestion places or address in google map and places using kotlin?

  android, android-studio, java, kotlin

I want to show suggestion places or address in my project using kotlin and didn’t see any errors on my codes but the suggestion predictions is not working on me. this my code look likes.

My variables

lateinit var mMap: GoogleMap
lateinit var placesClient: PlacesClient
lateinit var predictionList:List<AutocompletePrediction>
lateinit var mLastKnownLocation: Location
lateinit var locationCallback: LocationCallback

private var materialSearchBar: MaterialSearchBar?= null

private var DEFAULT_ZOOM = 18f

var mapView: View? = null
var mFusedLocationProviderClient: FusedLocationProviderClient? = null

And this is the code of suggestion places or addresses. By the way I’m using search bar material on my library.

private fun searchBar() {
        materialSearchBar?.setOnSearchActionListener(object :
            MaterialSearchBar.OnSearchActionListener {
            override fun onSearchStateChanged(enabled: Boolean) {
            }

            override fun onSearchConfirmed(text: CharSequence) {
                startSearch(text.toString(), true, null, true)
            }

            override fun onButtonClicked(buttonCode: Int) {
               }
            }
        })

        materialSearchBar?.addTextChangeListener(object: TextWatcher {
            val token = AutocompleteSessionToken.newInstance()
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val predictionsRequest = FindAutocompletePredictionsRequest.builder()
                    .setTypeFilter(TypeFilter.ADDRESS)
                    .setSessionToken(token)
                    .setQuery(s.toString())
                    .build()
                placesClient.findAutocompletePredictions(predictionsRequest).addOnCompleteListener(
                    object : OnCompleteListener<FindAutocompletePredictionsResponse>{
                        override fun onComplete(task: Task<FindAutocompletePredictionsResponse>) {
                            if(task.isSuccessful){
                                val predictionsResponse = task.result
                                if (predictionsRequest != null){
                                    predictionList = predictionsResponse.autocompletePredictions
                                    var suggestionsList: MutableList<String> = ArrayList()
                                    for (element in predictionList){
                                        suggestionsList.add(element.getFullText(null).toString())
                                    }
                                    materialSearchBar?.updateLastSuggestions(suggestionsList)
                                    if (!materialSearchBar!!.isSuggestionsVisible){
                                        materialSearchBar?.showSuggestionsList()
                                    }
                                }
                            }
                            else {
                                Log.i("Prediction", "Prediction fetching task unsuccessful")
                            }
                        }
                    })
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

Source: Android Questions

LEAVE A COMMENT