Search in sources :

Example 1 with Autocomplete

use of com.google.android.libraries.places.widget.Autocomplete in project android-places-demos by googlemaps.

the class PlaceAutocompleteActivity method programmaticPlacePredictions.

// [END maps_places_on_activity_result]
private void programmaticPlacePredictions(String query) {
    // [START maps_places_programmatic_place_predictions]
    // Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
    // and once again when the user makes a selection (for example when calling fetchPlace()).
    AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
    // Create a RectangularBounds object.
    RectangularBounds bounds = RectangularBounds.newInstance(new LatLng(-33.880490, 151.184363), new LatLng(-33.858754, 151.229596));
    // Use the builder to create a FindAutocompletePredictionsRequest.
    FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setLocationBias(bounds).setOrigin(new LatLng(-33.8749937, 151.2041382)).setCountries("AU", "NZ").setTypeFilter(TypeFilter.ADDRESS).setSessionToken(token).setQuery(query).build();
    placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
        for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
            Log.i(TAG, prediction.getPlaceId());
            Log.i(TAG, prediction.getPrimaryText(null).toString());
        }
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + apiException.getStatusCode());
        }
    });
// [END maps_places_programmatic_place_predictions]
}
Also used : TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) AutocompleteActivity(com.google.android.libraries.places.widget.AutocompleteActivity) Arrays(java.util.Arrays) Bundle(android.os.Bundle) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) Intent(android.content.Intent) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ArrayList(java.util.ArrayList) Log(android.util.Log) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) Places(com.google.android.libraries.places.api.Places) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Place(com.google.android.libraries.places.api.model.Place) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) List(java.util.List) Nullable(androidx.annotation.Nullable) Autocomplete(com.google.android.libraries.places.widget.Autocomplete) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) Status(com.google.android.gms.common.api.Status) NotNull(org.jetbrains.annotations.NotNull) ApiException(com.google.android.gms.common.api.ApiException) AutocompleteActivityMode(com.google.android.libraries.places.widget.model.AutocompleteActivityMode) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) LatLng(com.google.android.gms.maps.model.LatLng) ApiException(com.google.android.gms.common.api.ApiException)

Example 2 with Autocomplete

use of com.google.android.libraries.places.widget.Autocomplete in project android-places-demos by googlemaps.

the class PlaceAutocompleteActivity method startAutocompleteIntent.

// [START_EXCLUDE silent]
private void startAutocompleteIntent() {
    // [END_EXCLUDE]
    // Set the fields to specify which types of place data to
    // return after the user has made a selection.
    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
    // Start the autocomplete intent.
    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
// [END maps_places_autocomplete_intent]
}
Also used : Autocomplete(com.google.android.libraries.places.widget.Autocomplete) Intent(android.content.Intent)

Aggregations

Intent (android.content.Intent)2 Autocomplete (com.google.android.libraries.places.widget.Autocomplete)2 Bundle (android.os.Bundle)1 Log (android.util.Log)1 Nullable (androidx.annotation.Nullable)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 ApiException (com.google.android.gms.common.api.ApiException)1 Status (com.google.android.gms.common.api.Status)1 LatLng (com.google.android.gms.maps.model.LatLng)1 Places (com.google.android.libraries.places.api.Places)1 AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)1 AutocompleteSessionToken (com.google.android.libraries.places.api.model.AutocompleteSessionToken)1 Place (com.google.android.libraries.places.api.model.Place)1 RectangularBounds (com.google.android.libraries.places.api.model.RectangularBounds)1 TypeFilter (com.google.android.libraries.places.api.model.TypeFilter)1 FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)1 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)1 AutocompleteActivity (com.google.android.libraries.places.widget.AutocompleteActivity)1 AutocompleteSupportFragment (com.google.android.libraries.places.widget.AutocompleteSupportFragment)1 PlaceSelectionListener (com.google.android.libraries.places.widget.listener.PlaceSelectionListener)1