use of com.google.android.libraries.places.api.model.Place 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.Place in project open-event-orga-app by fossasia.
the class UpdateEventFragment method setupPlacesAutocomplete.
private void setupPlacesAutocomplete() {
ApplicationInfo ai = null;
try {
ai = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Bundle bundle = ai.metaData;
String placesApiKey = bundle.getString("com.google.android.geo.API_KEY");
Places.initialize(getActivity().getApplicationContext(), placesApiKey);
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Timber.d(place.getAddress());
Event event = binding.getEvent();
event.latitude = place.getLatLng().latitude;
event.longitude = place.getLatLng().longitude;
event.locationName = place.getAddress();
event.searchableLocationName = place.getName();
}
@Override
public void onError(Status status) {
ViewUtils.showSnackbar(binding.getRoot(), status.getStatusMessage());
}
});
}
use of com.google.android.libraries.places.api.model.Place in project open-event-orga-app by fossasia.
the class EventDetailsStepOne method setupPlacesAutocomplete.
private void setupPlacesAutocomplete() {
ApplicationInfo ai = null;
try {
ai = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Bundle bundle = ai.metaData;
String placesApiKey = bundle.getString("com.google.android.geo.API_KEY");
Places.initialize(getActivity().getApplicationContext(), placesApiKey);
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Timber.d(place.getAddress());
Event event = binding.getEvent();
event.latitude = place.getLatLng().latitude;
event.longitude = place.getLatLng().longitude;
event.locationName = place.getAddress();
event.searchableLocationName = place.getName();
}
@Override
public void onError(Status status) {
ViewUtils.showSnackbar(binding.getRoot(), status.getStatusMessage());
}
});
}
use of com.google.android.libraries.places.api.model.Place 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.model.Place in project android-places-demos by googlemaps.
the class PlaceAutocompleteActivity method onActivityResult.
// [START maps_places_on_activity_result]
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
Aggregations