简体   繁体   中英

Google's place autocomplete for android keeps giving me “Can't load search results”

I'm designing an app that uses maps and requires users to input destinations.i added the PlaceAutoCompleteFragment in the xml

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); } });

When I try searching it says:"Can't load search results".What should I do after this?

The autocomplete widget is a search dialog with built-in autocomplete functionality.

Use PlaceAutocomplete.IntentBuilder to create an intent to launch the autocomplete widget as an intent. After setting the optional parameters, call build(Activity) and pass the intent to 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.
}

To receive notification when a user has selected a place, your app should override the activity's onActivityResult(), checking for the request code you have passed for your intent, as shown in the following example.

@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.
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM