4

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.

enter image description here

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

enter image description here

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30

1 Answers1

3

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) {
           L.e("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

longi
  • 11,104
  • 10
  • 55
  • 89
  • i can customize places autocomplete but i want native google places code – Hitesh Gehlot Apr 19 '17 at 05:37
  • 1
    there is no "native google place" code for this behaviour. you have to implement it yourself. happy coding ;) – longi Apr 19 '17 at 12:13
  • 1
    You may use GeoDataApi.getAutocompletePredictions() [ https://developers.google.com/places/android-api/autocomplete#get_place_predictions_programmatically] for having native Android API – BhalchandraSW Apr 24 '17 at 10:07
  • @longilong: Could you please paste this method getLocationStringFrom(center) – Manu May 18 '19 at 10:27
  • @ManoharPerepa: No. Read the API docs where you'll immediately find that this is a required `Google Place API` parameter. `location` — The latitude/longitude around which to retrieve place information. This must be specified as latitude,longitude. – longi May 20 '19 at 12:53