谷歌的Android自动填充功能一直让我无法加载搜索结果"

时间:2016-07-16 17:21:54

标签: android google-maps autocomplete google-places-api

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

fragment
        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"
        />

And this is what is in my 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); } });

当我尝试搜索它时说:"无法加载搜索结果"。在此之后我该怎么办?

1 个答案:

答案 0 :(得分:-1)

autocomplete窗口小部件是一个内置自动填充功能的搜索对话框。

使用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.
}

要在用户选择地点时收到通知,您的应用应覆盖活动的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.
        }
    }
}