当我在 AutoCompleteTextView
(mSearchText) 中键入文本时,自动完成预测不会显示在我的 MapsActivity
中。
我尝试按照以下链接中的教程进行操作: https ://www.youtube.com/watch?v=6Trdd9EnmqY&list=PLgCYzUzKIBE-vInwQhGSdnbyJ62nixHCt&index=8。其中使用的一些方法最近已被弃用,因此我查看了 https://developers.google.com/places/android-sdk/autocomplete 上的文档,尤其是“以编程方式获取地点预测”和“迁移到新的 Places SDK 客户端” .我还按照同一个人的指南设置了我的 API 密钥,我认为这不应该是这里的问题。这是我的第一个 Android 应用程序,也是我在此站点上的第一个问题,所以请耐心等待。
Places.initialize(getApplicationContext(), getString(R.string.google_maps_API_key));
PlacesClient placesClient = Places.createClient(this);
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
.setLocationBias(RECTANGULAR_BOUNDS)
.setQuery(mSearchText.getText().toString())
.build();
placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
Log.i(TAG, prediction.getPlaceId());
Log.i(TAG, prediction.getPrimaryText(null).toString());
}
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
});
当用户在搜索栏中键入内容时,自动完成预测应该会出现,但事实并非如此。我也没有从听众那里得到任何日志。
原文由 Lorenzo Filizola 发布,翻译遵循 CC BY-SA 4.0 许可协议
新版本 2019
build.gradle(模块:应用程序)
活动布局
活动
适配器