如何从New Place SDK中的自动完成对象获取位置对象



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

下面给出了我的自动完成方法。如何从响应中获取位置对象?

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类具有getPlaceId()获取位置ID的方法。

要检索Place对象本身,必须使用呼叫PlacesClient.fetchPlace()使用位置ID检索位置详细信息。根据迁移指南:

呼叫fetchPlace()启动一个地方详细信息SKU,其中包括请求以及属于基本数据SKU的任何数据字段的成本。您还可以请求联系数据和气氛数据以额外收费。有关更多详细信息,请参见用法和计费。

因此,如果考虑到成本,您通常应该避免获得所有地点细节。

最新更新