Google的Android位置自动完成功能一直给我“无法加载搜索结果”

Jos*_*ith 7 android google-maps autocomplete google-places-api

我正在设计一个使用地图并需要用户输入目的地的应用程序。我在xml中添加了PlaceAutoCompleteFragment

分段
        android:id =“ @ + id / place_autocomplete_fragment”
        android:layout_width =“ 200dp”
        android:layout_height =“ wrap_content”
        android:layout_gravity =“ top” android:name =“ com.google.android.gms.location.places.ui.PlaceAutocompleteFragment”
        />

这就是我的java中的内容

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { @Override public void onPlaceSelected(Place place) { // TODO: Get info about the selected place. Log.i(TAG, "Place: " + place.getName()); } @Override public void onError(Status status) { // TODO: Handle the error. Log.i(TAG, "An error occurred: " + status); } });
Run Code Online (Sandbox Code Playgroud)

当我尝试搜索时,提示:“无法加载搜索结果”。此后该怎么办?

And*_*ast -2

自动完成小部件是一个具有内置自动完成功能的搜索对话框。

用于PlaceAutocomplete.IntentBuilder创建一个意图来启动自动完成小部件作为意图。设置可选参数后,调用build(Activity)并将意图传递给startActivityForResult(android.content.Intent, int).

int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
...
try {
    Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this);
    startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
    // TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
    // TODO: Handle the error.
}
Run Code Online (Sandbox Code Playgroud)

要在用户选择地点时接收通知,您的应用程序应重写活动的 onActivityResult(),检查您为您的意图传递的请求代码,如以下示例所示。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place: " + place.getName());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            // TODO: Handle the error.
            Log.i(TAG, status.getStatusMessage());    
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不会改变结果。唯一的区别是它启动了一个单独的自动完成活动。但结果是相同的。只是给我“无法加载搜索结果” (3认同)