Skip to content

Instantly share code, notes, and snippets.

@williaanlopes
Created August 1, 2019 02:05
Show Gist options
  • Save williaanlopes/6ac41ee5bf89f8e24a60717696e4f226 to your computer and use it in GitHub Desktop.
Save williaanlopes/6ac41ee5bf89f8e24a60717696e4f226 to your computer and use it in GitHub Desktop.
Android: find current user place using Google Places library 16.1.+
private final List<com.google.android.libraries.places.api.model.Place.Field> placeFields = Arrays.asList(
com.google.android.libraries.places.api.model.Place.Field.ID,
com.google.android.libraries.places.api.model.Place.Field.NAME,
com.google.android.libraries.places.api.model.Place.Field.ADDRESS,
com.google.android.libraries.places.api.model.Place.Field.LAT_LNG
);
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.builder(placeFields).build();
Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
placeResponse.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
FindCurrentPlaceResponse response = task.getResult();
if (response != null) {
if (response.getPlaceLikelihoods().size() > 0) {
Log.d(TAG, response.getPlaceLikelihoods().get(0).getPlace().getId());
Log.d(TAG, response.getPlaceLikelihoods().get(0).getPlace().getName());
Log.d(TAG, response.getPlaceLikelihoods().get(0).getPlace().getAddress());
Log.d(TAG, response.getPlaceLikelihoods().get(0).getPlace().getLatLng());
}
}
} else {
Exception exception = task.getException();
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getMessage());
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment