我已经实现了新的位置API,下面给出了我最近的位置列表方法。
private fun getNearestPlaceList(placesClient: PlacesClient) {
val placeFields = Arrays.asList(Place.Field.NAME)
val request = FindCurrentPlaceRequest.builder(placeFields).build()
placesClient.findCurrentPlace(request).addOnSuccessListener { response ->
placeList.clear()
for (placeLikelihood in response.placeLikelihoods) {
if(!placeLikelihood.place.id.isNullOrEmpty() && !placeLikelihood.place.name.isNullOrEmpty()){
var placeModel = PlaceModel(placeName = placeLikelihood.place.name!!, placeId = placeLikelihood.place.id!!)
placeList.add(placeModel)
}
}
setAdapter(placeList)
}.addOnFailureListener { exception ->
if (exception is ApiException) {
Log.e(TAG, "Place not found: " + exception.statusCode)
}
}
}
在此placeLikelihood.place.id始终返回null。有人知道,我们如何从可能性中获得位置ID?
您正在使用
val placeFields = Arrays.asList(Place.Field.NAME)
如果要返回ID,则需要包括Place.Field.ID
。