Skip to content

Instantly share code, notes, and snippets.

@jcbribeiro
Created March 26, 2019 19:39
Show Gist options
  • Select an option

  • Save jcbribeiro/fedd7d9f8cf827b86777d5e8aabf1002 to your computer and use it in GitHub Desktop.

Select an option

Save jcbribeiro/fedd7d9f8cf827b86777d5e8aabf1002 to your computer and use it in GitHub Desktop.
printNearbyPlaces using the new SDK
/*
Migrating to the New Places SDK Client
https://developers.google.com/places/android-sdk/client-migration#methods
*/
private void printNearbyPlaces() {
checkFineLocationPermission();
// Initialize Places.
Places.initialize(getApplicationContext(), MY_API_KEY);
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
// Use fields to define the data types to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.TYPES);
// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.builder(placeFields).build();
// Call findCurrentPlace and handle the response (first check that the user has granted permission).
placesClient.findCurrentPlace(request).addOnSuccessListener(new OnSuccessListener<FindCurrentPlaceResponse>() {
@Override
public void onSuccess(FindCurrentPlaceResponse response) {
String plText = "";
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
plText += "\t# " + placeLikelihood.getPlace().getName()
+ "\n\tlikelihood: " + placeLikelihood.getLikelihood()
+ "\n\taddress: " + placeLikelihood.getPlace().getAddress()
+ "\n\tplaceTypes: " + placeLikelihood.getPlace().getTypes() + "\n";
}
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String text = "\n\n[Places @ " + timestamp + "]\n" + plText;
textView_main.setText(text + textView_main.getText());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "e", Toast.LENGTH_SHORT).show();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment