How to customize PlaceAutocomplete widget dialog design to list places

后端 未结 4 1162
刺人心
刺人心 2020-12-13 01:01

I need to show list of places in dropdown using google placeAutocomplete widgets. Here I\'m getting dialog to show places according to my query but

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 01:19

    kotlin:

    fun queryPlaces(text: String) {
        val task = object : AsyncTask(){
            override fun doInBackground(vararg params: Void?): Void? {
                val results = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, text, null, null)
                val autocompletePredictions = results.await()
                val iterator = autocompletePredictions.iterator()
                while (iterator.hasNext()) {
                    val prediction = iterator.next()
                    println("PlaceId:${prediction.placeId}")
                    println("PrimaryText:${prediction.getPrimaryText(null)}")
                }
                autocompletePredictions.release()
                return null
            }
        }
        task.execute()
    }
    

    If you want get the Place detail:

    fun queryPlaces(text: String) {
        val geoDataClient : GeoDataClient = Places.getGeoDataClient(context!!)
    
        val task = object : AsyncTask(){
            override fun doInBackground(vararg params: Void?): Void? {
                val results = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, text, null, null)
                val autocompletePredictions = results.await()
                val iterator = autocompletePredictions.iterator()
                while (iterator.hasNext()) {
                    val prediction = iterator.next()
    
                    geoDataClient.getPlaceById(prediction.placeId).addOnCompleteListener {task->
                        val places = task.result
                        val myPlace = places?.get(0)
                        println("name:${myPlace?.name}")
                        println("latLng:${myPlace?.latLng}")
                        println("address:${myPlace?.address}")
                    }
                }
                autocompletePredictions.release()
                return null
            }
        }
        task.execute()
    }
    

提交回复
热议问题