use of com.google.android.libraries.places.api.model.PlaceLikelihood in project android-places-demos by googlemaps.
the class CurrentPlaceActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [START maps_places_current_place]
// Use fields to define the data types to return.
List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME);
// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
// Call findCurrentPlace and handle the response (first check that the user has granted permission).
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
placeResponse.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
FindCurrentPlaceResponse response = task.getResult();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
Log.i(TAG, String.format("Place '%s' has likelihood: %f", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood()));
}
} else {
Exception exception = task.getException();
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
}
});
} else {
// A local method to request required permissions;
// See https://developer.android.com/training/permissions/requesting
getLocationPermission();
}
// [END maps_places_current_place]
}
use of com.google.android.libraries.places.api.model.PlaceLikelihood in project android-samples by googlemaps.
the class MapsActivityCurrentPlace method showCurrentPlace.
// [END maps_current_place_on_request_permissions_result]
/**
* Prompts the user to select the current place from a list of likely places, and shows the
* current place on the map - provided the user has granted location permission.
*/
// [START maps_current_place_show_current_place]
private void showCurrentPlace() {
if (map == null) {
return;
}
if (locationPermissionGranted) {
// Use fields to define the data types to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);
// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
// Get the likely places - that is, the businesses and other points of interest that
// are the best match for the device's current location.
@SuppressWarnings("MissingPermission") final Task<FindCurrentPlaceResponse> placeResult = placesClient.findCurrentPlace(request);
placeResult.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
@Override
public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
if (task.isSuccessful() && task.getResult() != null) {
FindCurrentPlaceResponse likelyPlaces = task.getResult();
// Set the count, handling cases where less than 5 entries are returned.
int count;
if (likelyPlaces.getPlaceLikelihoods().size() < M_MAX_ENTRIES) {
count = likelyPlaces.getPlaceLikelihoods().size();
} else {
count = M_MAX_ENTRIES;
}
int i = 0;
likelyPlaceNames = new String[count];
likelyPlaceAddresses = new String[count];
likelyPlaceAttributions = new List[count];
likelyPlaceLatLngs = new LatLng[count];
for (PlaceLikelihood placeLikelihood : likelyPlaces.getPlaceLikelihoods()) {
// Build a list of likely places to show the user.
likelyPlaceNames[i] = placeLikelihood.getPlace().getName();
likelyPlaceAddresses[i] = placeLikelihood.getPlace().getAddress();
likelyPlaceAttributions[i] = placeLikelihood.getPlace().getAttributions();
likelyPlaceLatLngs[i] = placeLikelihood.getPlace().getLatLng();
i++;
if (i > (count - 1)) {
break;
}
}
// Show a dialog offering the user the list of likely places, and add a
// marker at the selected place.
MapsActivityCurrentPlace.this.openPlacesDialog();
} else {
Log.e(TAG, "Exception: %s", task.getException());
}
}
});
} else {
// The user has not granted permission.
Log.i(TAG, "The user did not grant location permission.");
// Add a default marker, because the user hasn't selected a place.
map.addMarker(new MarkerOptions().title(getString(R.string.default_info_title)).position(defaultLocation).snippet(getString(R.string.default_info_snippet)));
// Prompt the user for permission.
getLocationPermission();
}
}
use of com.google.android.libraries.places.api.model.PlaceLikelihood in project react-native-google-places by tolu360.
the class RNGooglePlacesModule method findCurrentPlaceWithPermissions.
/**
* Fetches a list of {@link PlaceLikelihood} instances that represent the Places the user is
* most
* likely to be at currently.
*/
@RequiresPermission(allOf = { ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE })
private void findCurrentPlaceWithPermissions(List<Place.Field> selectedFields, final Promise promise) {
FindCurrentPlaceRequest currentPlaceRequest = FindCurrentPlaceRequest.newInstance(selectedFields);
Task<FindCurrentPlaceResponse> currentPlaceTask = placesClient.findCurrentPlace(currentPlaceRequest);
currentPlaceTask.addOnSuccessListener((response) -> {
if (response.getPlaceLikelihoods().size() == 0) {
WritableArray emptyResult = Arguments.createArray();
promise.resolve(emptyResult);
return;
}
WritableArray likelyPlacesList = Arguments.createArray();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
WritableMap map = propertiesMapForPlace(placeLikelihood.getPlace(), selectedFields);
map.putDouble("likelihood", placeLikelihood.getLikelihood());
likelyPlacesList.pushMap(map);
}
promise.resolve(likelyPlacesList);
});
currentPlaceTask.addOnFailureListener((exception) -> {
promise.reject("E_CURRENT_PLACE_ERROR", new Error(exception.getMessage()));
});
}
use of com.google.android.libraries.places.api.model.PlaceLikelihood in project android-places-demos by googlemaps.
the class StringUtil method stringify.
static String stringify(FindCurrentPlaceResponse response, boolean raw) {
StringBuilder builder = new StringBuilder();
builder.append(response.getPlaceLikelihoods().size()).append(" Current Place Results:");
if (raw) {
builder.append(RESULT_SEPARATOR);
appendListToStringBuilder(builder, response.getPlaceLikelihoods());
} else {
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
builder.append(RESULT_SEPARATOR).append("Likelihood: ").append(placeLikelihood.getLikelihood()).append(FIELD_SEPARATOR).append("Place: ").append(stringify(placeLikelihood.getPlace()));
}
}
return builder.toString();
}
Aggregations