use of com.google.android.libraries.places.api.net.FetchPlaceRequest in project android-places-demos by googlemaps.
the class PlaceDetailsActivity method getPlaceById.
private void getPlaceById() {
// [START maps_places_get_place_by_id]
// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";
// Specify the fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Construct a request object, passing the place ID and fields array.
final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
Place place = response.getPlace();
Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
});
// [END maps_places_get_place_by_id]
}
use of com.google.android.libraries.places.api.net.FetchPlaceRequest 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.net.FetchPlaceRequest in project hypertrack-live-android by hypertrack.
the class SearchPlacePresenter method selectItem.
public void selectItem(PlaceModel placeModel) {
view.showProgressBar();
if (!placeModel.isRecent) {
state.addPlaceToRecent(placeModel);
}
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS);
FetchPlaceRequest request = FetchPlaceRequest.builder(placeModel.placeId, fields).setSessionToken(token).build();
placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
@Override
public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
view.hideProgressBar();
PlaceModel destination = new PlaceModel();
destination.address = fetchPlaceResponse.getPlace().getAddress();
destination.latLng = fetchPlaceResponse.getPlace().getLatLng();
state.setDestination(destination);
providePlace(destination);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
view.hideProgressBar();
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.FetchPlaceRequest in project android-places-demos by googlemaps.
the class PlaceAndPhotoTestActivity method fetchPlace.
/**
* Fetches the {@link Place} specified via the UI and displays it. May also trigger {@link
* #fetchPhoto(PhotoMetadata)} if set in the UI.
*/
private void fetchPlace() {
responseView.setText(null);
photoView.setImageBitmap(null);
dismissKeyboard(findViewById(R.id.place_id_field));
final boolean isFetchPhotoChecked = isFetchPhotoChecked();
List<Field> placeFields = getPlaceFields();
String customPhotoReference = getCustomPhotoReference();
if (!validateInputs(isFetchPhotoChecked, placeFields, customPhotoReference)) {
return;
}
setLoading(true);
FetchPlaceRequest request = FetchPlaceRequest.newInstance(getPlaceId(), placeFields);
Task<FetchPlaceResponse> placeTask = placesClient.fetchPlace(request);
placeTask.addOnSuccessListener((response) -> {
responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked()));
if (isFetchPhotoChecked) {
attemptFetchPhoto(response.getPlace());
}
});
placeTask.addOnFailureListener((exception) -> {
exception.printStackTrace();
responseView.setText(exception.getMessage());
});
placeTask.addOnCompleteListener(response -> setLoading(false));
}
use of com.google.android.libraries.places.api.net.FetchPlaceRequest in project android-places-demos by googlemaps.
the class PlacePhotosActivity method getPlacePhoto.
private void getPlacePhoto() {
// [START maps_places_get_place_photos]
// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";
// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
final List<Place.Field> fields = Collections.singletonList(Place.Field.PHOTO_METADATAS);
// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
final FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);
placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
final Place place = response.getPlace();
// Get the photo metadata.
final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
if (metadata == null || metadata.isEmpty()) {
Log.w(TAG, "No photo metadata.");
return;
}
final PhotoMetadata photoMetadata = metadata.get(0);
// Get the attribution text.
final String attributions = photoMetadata.getAttributions();
// Create a FetchPhotoRequest.
final FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxWidth(// Optional.
500).setMaxHeight(// Optional.
300).build();
placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
Bitmap bitmap = fetchPhotoResponse.getBitmap();
imageView.setImageBitmap(bitmap);
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
});
});
// [END maps_places_get_place_photos]
}
Aggregations