Search in sources :

Example 1 with LocationBias

use of com.google.android.libraries.places.api.model.LocationBias in project android-places-demos by googlemaps.

the class ProgrammaticAutocompleteToolbarActivity method getPlacePredictions.

/**
 * This method demonstrates the programmatic approach to getting place predictions. The
 * parameters in this request are currently biased to Kolkata, India.
 *
 * @param query the plus code query string (e.g. "GCG2+3M K")
 */
private void getPlacePredictions(String query) {
    // The value of 'bias' biases prediction results to the rectangular region provided
    // (currently Kolkata). Modify these values to get results for another area. Make sure to
    // pass in the appropriate value/s for .setCountries() in the
    // FindAutocompletePredictionsRequest.Builder object as well.
    final LocationBias bias = RectangularBounds.newInstance(// SW lat, lng
    new LatLng(22.458744, 88.208162), // NE lat, lng
    new LatLng(22.730671, 88.524896));
    // Create a new programmatic Place Autocomplete request in Places SDK for Android
    final FindAutocompletePredictionsRequest newRequest = FindAutocompletePredictionsRequest.builder().setSessionToken(sessionToken).setLocationBias(bias).setTypeFilter(TypeFilter.ESTABLISHMENT).setQuery(query).setCountries("IN").build();
    // Perform autocomplete predictions request
    placesClient.findAutocompletePredictions(newRequest).addOnSuccessListener((response) -> {
        List<AutocompletePrediction> predictions = response.getAutocompletePredictions();
        adapter.setPredictions(predictions);
        progressBar.setIndeterminate(false);
        viewAnimator.setDisplayedChild(predictions.isEmpty() ? 0 : 1);
    }).addOnFailureListener((exception) -> {
        progressBar.setIndeterminate(false);
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + apiException.getStatusCode());
        }
    });
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) Volley(com.android.volley.toolbox.Volley) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) GeocodingResult(com.example.placesdemo.model.GeocodingResult) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) GsonBuilder(com.google.gson.GsonBuilder) MenuItem(android.view.MenuItem) JSONException(org.json.JSONException) R(com.example.placesdemo.R) Gson(com.google.gson.Gson) Handler(android.os.Handler) Menu(android.view.Menu) RecyclerView(androidx.recyclerview.widget.RecyclerView) Log(android.util.Log) LocationBias(com.google.android.libraries.places.api.model.LocationBias) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Method(com.android.volley.Request.Method) SearchView(android.widget.SearchView) RequestQueue(com.android.volley.RequestQueue) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) MainActivity(com.example.placesdemo.MainActivity) LatLng(com.google.android.libraries.maps.model.LatLng) Places(com.google.android.libraries.places.api.Places) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) List(java.util.List) Nullable(androidx.annotation.Nullable) BuildConfig(com.example.placesdemo.BuildConfig) ViewAnimator(android.widget.ViewAnimator) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) OnQueryTextListener(android.widget.SearchView.OnQueryTextListener) ApiException(com.google.android.gms.common.api.ApiException) JSONArray(org.json.JSONArray) LocationBias(com.google.android.libraries.places.api.model.LocationBias) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) List(java.util.List) LatLng(com.google.android.libraries.maps.model.LatLng) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Log (android.util.Log)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 ProgressBar (android.widget.ProgressBar)1 SearchView (android.widget.SearchView)1 OnQueryTextListener (android.widget.SearchView.OnQueryTextListener)1 ViewAnimator (android.widget.ViewAnimator)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 Method (com.android.volley.Request.Method)1 RequestQueue (com.android.volley.RequestQueue)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 Volley (com.android.volley.toolbox.Volley)1