简体   繁体   中英

android Place Autocomplete how to get list of searched place on top by default

I implement place autocomplete in my application and write search keyword in EditText to get result according to search place keyword. here the code for start PlaceAutocomplete activity to search place

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) {
} catch (GooglePlayServicesNotAvailableException e) {
}

when activity launch list showing blank.

在此输入图像描述

What I want- already searched place should be show when activity launch like uber app

在此输入图像描述

You have to persistent your autocomplete results within internal app storage (shared preferences, database..) or somewhere you want. Basically, you need to implement your UI yourself. I don't think you can customise the PlaceAutocomplete.IntentBuilder for your needs.

  1. Implement API-Call for AutocompleteGooglePlaces (we are using retrofit for this)

     @GET("/maps/api/place/autocomplete/json") Call<GoogleAutocompleteResponse> getAutoCompleteSearchResults(@Query("key") String apiKey, @Query("input") String searchTerm, @Query("location") String location, @Query("radius") long radius); 
  2. Perform call (on Enter-click or onTextChange)

     public List<GooglePlaceAutocompleteGeoModel> getAutoCompleteSearchResults(String searchTerm, LatLng center, long radius) { Call<GoogleAutocompleteResponse> call = googlePlacesRestApiService.getAutoCompleteSearchResults(API_KEY, searchTerm, getLocationStringFrom(center), radius); try { GoogleAutocompleteResponse autocompleteResponse = call.execute().body(); List<GooglePlaceAutocompleteGeoModel> autocompleteResponsePredictions = autocompleteResponse.getPredictions(); //Here are your Autocomplete Objects return autocompleteResponsePredictions; } catch (IOException e) { Le("Error on Google Autocomplete call: " + e.getMessage()); } return new ArrayList<>(); } 
  3. Persistent the result of autocomplete result GoogleAutocompleteResponse within your app and implement logic for showing results in UI or populated within ListView

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