如何让这个 placeRequest 获取预测的第一个结果?
How do I make this placeRequest take the first result of the predictions?
我必须传递从 findAutocompletePredictions(userInputRequest)
到 placeRequest
的第一个结果(索引 0),但我无法访问 predictions
,因为它是在内部声明的addOnSuccessListener
并且如果我尝试拥有一个全局 属性 并将其传递给 placeRequest
因为 findAutocompletePredictions
是异步的,那么全局 属性 要么为 null 要么初始化传递给 placeRequest
时的值
class PlacesApi(context: Context, apiKey: String) {
private val placesClient: PlacesClient
private val token: AutocompleteSessionToken
private lateinit var place: Place
init {
Places.initialize(context, apiKey)
placesClient = Places.createClient(context)
token = AutocompleteSessionToken.newInstance()
}
fun getCityPlaceId(inputString: String) {
val userInputRequest = FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.CITIES)
.setSessionToken(token)
.setQuery(inputString)
.build()
placesClient.findAutocompletePredictions(userInputRequest)
.addOnSuccessListener { response: FindAutocompletePredictionsResponse ->
for (prediction in response.autocompletePredictions) {
Log.d("Place ID", prediction.placeId)
Log.d("Place City", prediction.getPrimaryText(null).toString())
Log.d("Place Country", prediction.getSecondaryText(null).toString())
}
}
val placeFields: List<Place.Field> =
listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
// I need to change this hard-coded string value to the fist result of the predictions
val placeRequest = FetchPlaceRequest.newInstance("ChIJD7fiBh9u5kcRYJSMaMOCCwQ", placeFields)
placesClient.fetchPlace(placeRequest)
.addOnSuccessListener { fetchPlaceResponse: FetchPlaceResponse ->
place = fetchPlaceResponse.place
Log.i("Place coordinates: ", place.latLng?.longitude.toString())
Log.i("Place coordinates: ", place.latLng?.latitude.toString())
Log.i("Place name: ", place.name.toString())
Log.i("Place address: ", place.address.toString())
}.addOnFailureListener {
if (it is ApiException) {
Log.e("Place not found", "Place not found " + it.message)
}
}
}
}
由于 findAutocompletePredictions
是异步的,因此在 findAutocompletePredictions
的结果可用之前执行 placesClient.fetchPlace
没有意义。
你应该做的是 findAutocompletePredictions
的 onSuccess 调用另一个函数并将 findAutocompletePredictions
的结果传递给它,然后第二个函数可以为你取地方
fun getCityPlaceId(inputString: String) {
val userInputRequest = FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.CITIES)
.setSessionToken(token)
.setQuery(inputString)
.build()
// on success pass the result to another function which can
// take care of fetching the place information
// you can pass entire result or the only part required to get place info
placesClient.findAutocompletePredictions(userInputRequest)
.addOnSuccessListener { fetchPlace(it) }
}
private fun fetchPlace(response: FindAutocompletePredictionsResponse){
val placeFields: List<Place.Field> =
listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
val placeRequest = FetchPlaceRequest.newInstance(response.autocompletePredictions[0].placeId, placeFields)
placesClient.fetchPlace(placeRequest)
.addOnSuccessListener { fetchPlaceResponse: FetchPlaceResponse ->
place = fetchPlaceResponse.place
Log.i("Place coordinates: ", place.latLng?.longitude.toString())
Log.i("Place coordinates: ", place.latLng?.latitude.toString())
Log.i("Place name: ", place.name.toString())
Log.i("Place address: ", place.address.toString())
}.addOnFailureListener {
if (it is ApiException) {
Log.e("Place not found", "Place not found " + it.message)
}
}
}
我必须传递从 findAutocompletePredictions(userInputRequest)
到 placeRequest
的第一个结果(索引 0),但我无法访问 predictions
,因为它是在内部声明的addOnSuccessListener
并且如果我尝试拥有一个全局 属性 并将其传递给 placeRequest
因为 findAutocompletePredictions
是异步的,那么全局 属性 要么为 null 要么初始化传递给 placeRequest
class PlacesApi(context: Context, apiKey: String) {
private val placesClient: PlacesClient
private val token: AutocompleteSessionToken
private lateinit var place: Place
init {
Places.initialize(context, apiKey)
placesClient = Places.createClient(context)
token = AutocompleteSessionToken.newInstance()
}
fun getCityPlaceId(inputString: String) {
val userInputRequest = FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.CITIES)
.setSessionToken(token)
.setQuery(inputString)
.build()
placesClient.findAutocompletePredictions(userInputRequest)
.addOnSuccessListener { response: FindAutocompletePredictionsResponse ->
for (prediction in response.autocompletePredictions) {
Log.d("Place ID", prediction.placeId)
Log.d("Place City", prediction.getPrimaryText(null).toString())
Log.d("Place Country", prediction.getSecondaryText(null).toString())
}
}
val placeFields: List<Place.Field> =
listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
// I need to change this hard-coded string value to the fist result of the predictions
val placeRequest = FetchPlaceRequest.newInstance("ChIJD7fiBh9u5kcRYJSMaMOCCwQ", placeFields)
placesClient.fetchPlace(placeRequest)
.addOnSuccessListener { fetchPlaceResponse: FetchPlaceResponse ->
place = fetchPlaceResponse.place
Log.i("Place coordinates: ", place.latLng?.longitude.toString())
Log.i("Place coordinates: ", place.latLng?.latitude.toString())
Log.i("Place name: ", place.name.toString())
Log.i("Place address: ", place.address.toString())
}.addOnFailureListener {
if (it is ApiException) {
Log.e("Place not found", "Place not found " + it.message)
}
}
}
}
由于 findAutocompletePredictions
是异步的,因此在 findAutocompletePredictions
的结果可用之前执行 placesClient.fetchPlace
没有意义。
你应该做的是 findAutocompletePredictions
的 onSuccess 调用另一个函数并将 findAutocompletePredictions
的结果传递给它,然后第二个函数可以为你取地方
fun getCityPlaceId(inputString: String) {
val userInputRequest = FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.CITIES)
.setSessionToken(token)
.setQuery(inputString)
.build()
// on success pass the result to another function which can
// take care of fetching the place information
// you can pass entire result or the only part required to get place info
placesClient.findAutocompletePredictions(userInputRequest)
.addOnSuccessListener { fetchPlace(it) }
}
private fun fetchPlace(response: FindAutocompletePredictionsResponse){
val placeFields: List<Place.Field> =
listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
val placeRequest = FetchPlaceRequest.newInstance(response.autocompletePredictions[0].placeId, placeFields)
placesClient.fetchPlace(placeRequest)
.addOnSuccessListener { fetchPlaceResponse: FetchPlaceResponse ->
place = fetchPlaceResponse.place
Log.i("Place coordinates: ", place.latLng?.longitude.toString())
Log.i("Place coordinates: ", place.latLng?.latitude.toString())
Log.i("Place name: ", place.name.toString())
Log.i("Place address: ", place.address.toString())
}.addOnFailureListener {
if (it is ApiException) {
Log.e("Place not found", "Place not found " + it.message)
}
}
}