use of com.google.android.libraries.places.api.model.AutocompletePrediction 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]
}
use of com.google.android.libraries.places.api.model.AutocompletePrediction in project android-places-demos by googlemaps.
the class PlacePredictionAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull PlacePredictionViewHolder holder, int position) {
final AutocompletePrediction prediction = predictions.get(position);
holder.setPrediction(prediction);
holder.itemView.setOnClickListener(v -> {
if (onPlaceClickListener != null) {
onPlaceClickListener.onPlaceClicked(prediction);
}
});
}
use of com.google.android.libraries.places.api.model.AutocompletePrediction in project iNaturalistAndroid by inaturalist.
the class LocationChooserActivity method getPlaceDetails.
private void getPlaceDetails(AutocompletePrediction prediction, int index, CountDownLatch latch) {
new Thread(() -> {
if (prediction.getPlaceId() == null) {
// We only show predictions with place IDs (so we can retrieve exact lat/lng)
latch.countDown();
return;
}
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.VIEWPORT);
FetchPlaceRequest request = FetchPlaceRequest.builder(prediction.getPlaceId(), fields).build();
mPlacesClient.fetchPlace(request).addOnSuccessListener(fetchPlaceResponse -> {
// Only when successfully fetching the place details -> add it to the results list
Place googlePlace = fetchPlaceResponse.getPlace();
LatLngBounds viewport = googlePlace.getViewport();
Double radius = null;
Double latitude = googlePlace.getLatLng().latitude;
Double longitude = googlePlace.getLatLng().longitude;
if (viewport != null) {
LatLng center = viewport.getCenter();
LatLng northeast = viewport.northeast;
// Radius is the largest distance from geom center to one of the bounds corners
radius = Math.max(distanceInMeters(latitude, longitude, center.latitude, center.longitude), distanceInMeters(latitude, longitude, northeast.latitude, northeast.longitude));
} else {
radius = 10.0;
}
if (index < mPlaces.size()) {
mPlaces.get(index).accuracy = radius;
mPlaces.get(index).latitude = latitude;
mPlaces.get(index).longitude = longitude;
}
latch.countDown();
}).addOnFailureListener(e -> {
latch.countDown();
});
}).start();
}
use of com.google.android.libraries.places.api.model.AutocompletePrediction in project iNaturalistAndroid by inaturalist.
the class LocationChooserActivity method loadPlaceResults.
private void loadPlaceResults(List<AutocompletePrediction> predictions) {
mWaitForAllResults = new CountDownLatch(predictions.size());
mPlaces = new ArrayList<>();
for (AutocompletePrediction prediction : predictions) {
INatPlace inatPlace = new INatPlace();
inatPlace.id = prediction.getPlaceId();
inatPlace.title = prediction.getPrimaryText(null).toString();
inatPlace.subtitle = prediction.getSecondaryText(null).toString();
mPlaces.add(inatPlace);
getPlaceDetails(prediction, mPlaces.size() - 1, mWaitForAllResults);
}
// Refresh results
mPlaceAdapter = new LocationChooserPlaceAdapter(this, mPlaces);
runOnUiThread(() -> {
mLocationList.setAdapter(mPlaceAdapter);
mLoadingSearch.setVisibility(View.GONE);
mLocationList.setVisibility(View.VISIBLE);
});
}
use of com.google.android.libraries.places.api.model.AutocompletePrediction 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());
}
});
}
Aggregations