use of com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest 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.net.FindAutocompletePredictionsRequest in project hypertrack-live-android by hypertrack.
the class SearchPlacePresenter method search.
public void search(String query) {
if (!state.mapDestinationMode) {
if (TextUtils.isEmpty(query)) {
view.updateList(state.getRecentPlaces());
} else {
// 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 selectPlace()).
token = AutocompleteSessionToken.newInstance();
// Use the builder to create a FindAutocompletePredictionsRequest.
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setTypeFilter(TypeFilter.GEOCODE).setSessionToken(token).setQuery(query).build();
placesClient.findAutocompletePredictions(request).addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
@Override
public void onSuccess(FindAutocompletePredictionsResponse response) {
view.updateList(PlaceModel.from(response.getAutocompletePredictions()));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
}
});
}
}
}
use of com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest in project iNaturalistAndroid by inaturalist.
the class LocationChooserActivity method refreshPlaceQuery.
private void refreshPlaceQuery() {
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(() -> {
if (mMap == null)
return;
String query = mLocationSearch.getText().toString();
if (query.length() == 0) {
mLoadingSearch.setVisibility(View.GONE);
mLocationList.setVisibility(View.VISIBLE);
return;
}
mLoadingSearch.setVisibility(View.VISIBLE);
mLocationList.setVisibility(View.GONE);
VisibleRegion region = mMap.getProjection().getVisibleRegion();
RectangularBounds bounds;
if (region.farRight.latitude >= region.nearLeft.latitude) {
bounds = RectangularBounds.newInstance(region.nearLeft, region.farRight);
} else {
bounds = RectangularBounds.newInstance(region.farRight, region.nearLeft);
}
if (query.matches(REGEX_LAT_LNG)) {
// Lat/lng search (not a place search)
double lat = Double.valueOf(query.split(",")[0].trim());
double lng = Double.valueOf(query.split(",")[1].trim());
Logger.tag(TAG).info("Location search for lat/lng: " + lat + "/" + lng);
mWaitForAllResults = new CountDownLatch(1);
mPlaces = new ArrayList<>();
INatPlace inatPlace = new INatPlace();
inatPlace.id = null;
inatPlace.title = String.format(getString(R.string.location_lat_lng), lat, lng);
inatPlace.subtitle = null;
inatPlace.latitude = lat;
inatPlace.longitude = lng;
inatPlace.accuracy = Double.valueOf(0);
mPlaces.add(inatPlace);
// Refresh results
mPlaceAdapter = new LocationChooserPlaceAdapter(this, mPlaces);
runOnUiThread(() -> {
mLocationList.setAdapter(mPlaceAdapter);
mLoadingSearch.setVisibility(View.GONE);
mLocationList.setVisibility(View.VISIBLE);
});
mWaitForAllResults.countDown();
return;
}
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setSessionToken(mAutoCompleteToken).setLocationBias(bounds).setQuery(query).build();
mQuery = query;
mPlacesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
if (!query.equals(mQuery))
return;
new Thread(() -> loadPlaceResults(response.getAutocompletePredictions())).start();
}).addOnFailureListener((exception) -> {
Logger.tag(TAG).error("Place not found: " + exception);
});
}, 500);
}
use of com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest 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