如何从 New Place SDK 中的 autocompletePredictions 获取 Place 对象

How to get the Place Object from autocompletePredictions in New Place SDK

我已经集成了Google提供的新Place SDK。

我的自动补全方法如下。如何从响应中获取 Place 对象?

private fun getPlaceAutoComplete(placesClient: PlacesClient, query: String) {
        val token = AutocompleteSessionToken.newInstance()
        val request = FindAutocompletePredictionsRequest.builder()
                .setSessionToken(token)
                .setQuery(query)
                .build()
        placesClient.findAutocompletePredictions(request).addOnSuccessListener { response ->
            placeList.clear()// Place List is a List of Place Instances
            for (prediction in response.autocompletePredictions) {

                var place = prediction.getPlace()// There is no option to ge the Place.
                placeList.add(place)
            }
            setAdapter(placeList)
        }.addOnFailureListener { exception ->
            if (exception is ApiException) {
                Log.e(TAG, "Place not found: " + exception.statusCode)
            }
        }

    }

AutoCompletePrediction class 有一个 getPlaceId() method for getting a Place ID.

要检索 Place 对象本身,您必须 Retrieve the place details using the place ID by using a call to PlacesClient.fetchPlace(). As per the Migration guide:

Calls to fetchPlace() initiate a Places Details SKU, the cost for which includes the request and any data fields belonging to the Basic Data SKU. You can also request Contact Data and Atmosphere Data for an additional charge. See Usage and Billing for more details.

因此,如果担心成本,您通常应该避免获取所有地点详细信息。