use of com.google.android.gms.location.places.PlaceLikelihood in project Android-ReactiveLocation by mcharmas.
the class PlacesActivity method onLocationPermissionGranted.
@Override
protected void onLocationPermissionGranted() {
compositeSubscription = new CompositeSubscription();
compositeSubscription.add(reactiveLocationProvider.getCurrentPlace(null).subscribe(new Action1<PlaceLikelihoodBuffer>() {
@Override
public void call(PlaceLikelihoodBuffer buffer) {
PlaceLikelihood likelihood = buffer.get(0);
if (likelihood != null) {
currentPlaceView.setText(likelihood.getPlace().getName());
}
buffer.release();
}
}));
Observable<String> queryObservable = RxTextView.textChanges(queryView).map(new Func1<CharSequence, String>() {
@Override
public String call(CharSequence charSequence) {
return charSequence.toString();
}
}).debounce(1, TimeUnit.SECONDS).filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return !TextUtils.isEmpty(s);
}
});
Observable<Location> lastKnownLocationObservable = reactiveLocationProvider.getLastKnownLocation();
Observable<AutocompletePredictionBuffer> suggestionsObservable = Observable.combineLatest(queryObservable, lastKnownLocationObservable, new Func2<String, Location, QueryWithCurrentLocation>() {
@Override
public QueryWithCurrentLocation call(String query, Location currentLocation) {
return new QueryWithCurrentLocation(query, currentLocation);
}
}).flatMap(new Func1<QueryWithCurrentLocation, Observable<AutocompletePredictionBuffer>>() {
@Override
public Observable<AutocompletePredictionBuffer> call(QueryWithCurrentLocation q) {
if (q.location == null)
return Observable.empty();
double latitude = q.location.getLatitude();
double longitude = q.location.getLongitude();
LatLngBounds bounds = new LatLngBounds(new LatLng(latitude - 0.05, longitude - 0.05), new LatLng(latitude + 0.05, longitude + 0.05));
return reactiveLocationProvider.getPlaceAutocompletePredictions(q.query, bounds, null);
}
});
compositeSubscription.add(suggestionsObservable.subscribe(new Action1<AutocompletePredictionBuffer>() {
@Override
public void call(AutocompletePredictionBuffer buffer) {
List<AutocompleteInfo> infos = new ArrayList<>();
for (AutocompletePrediction prediction : buffer) {
infos.add(new AutocompleteInfo(prediction.getFullText(null).toString(), prediction.getPlaceId()));
}
buffer.release();
placeSuggestionsList.setAdapter(new ArrayAdapter<>(PlacesActivity.this, android.R.layout.simple_list_item_1, infos));
}
}));
}
use of com.google.android.gms.location.places.PlaceLikelihood in project UniPool by divya21raj.
the class NewEntryActivity method getCurrentPlace.
private void getCurrentPlace() {
final Place[] place = new Place[1];
final LatLng[] latLng = new LatLng[1];
@SuppressLint("MissingPermission") PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(likelyPlaces -> {
try {
PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
place[0] = placeLikelihood.getPlace();
latLng[0] = place[0].getLatLng();
if (currentLocationView.getTag().equals("gct_source")) {
source = new GenLocation(place[0].getName().toString(), place[0].getAddress().toString(), latLng[0].latitude, latLng[0].longitude);
sourceSet = (place[0].getName() + ",\n" + place[0].getAddress() + "\n" + // check
place[0].getPhoneNumber());
findSource.setText(sourceSet);
} else if (currentLocationView.getTag().equals("gct_destination")) {
destination = new GenLocation(place[0].getName().toString(), place[0].getAddress().toString(), latLng[0].latitude, latLng[0].longitude);
destinationSet = (place[0].getName() + ",\n" + place[0].getAddress() + "\n" + // check
place[0].getPhoneNumber());
findDestination.setText(destinationSet);
}
likelyPlaces.release();
} catch (IllegalStateException ils) {
Toast.makeText(getApplicationContext(), "Turn on 'Location' option on your phone to use this feature...", Toast.LENGTH_LONG).show();
findSource.setText("Source");
findDestination.setText("Destination");
}
});
}
use of com.google.android.gms.location.places.PlaceLikelihood in project react-native-google-places by tolu360.
the class RNGooglePlacesModule method getCurrentPlace.
@ReactMethod
public void getCurrentPlace(final Promise promise) {
PendingResult<PlaceLikelihoodBuffer> results = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
PlaceLikelihoodBuffer likelyPlaces = results.await(60, TimeUnit.SECONDS);
final Status status = likelyPlaces.getStatus();
if (status.isSuccess()) {
if (likelyPlaces.getCount() == 0) {
WritableArray emptyResult = Arguments.createArray();
likelyPlaces.release();
promise.resolve(emptyResult);
return;
}
WritableArray likelyPlacesList = Arguments.createArray();
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
WritableMap map = propertiesMapForPlace(placeLikelihood.getPlace());
map.putDouble("likelihood", placeLikelihood.getLikelihood());
likelyPlacesList.pushMap(map);
}
// Release the buffer now that all data has been copied.
likelyPlaces.release();
promise.resolve(likelyPlacesList);
} else {
Log.i(TAG, "Error making places detection api call: " + status.getStatusMessage());
likelyPlaces.release();
promise.reject("E_PLACE_DETECTION_API_ERROR", new Error("Error making places detection api call: " + status.getStatusMessage()));
return;
}
}
use of com.google.android.gms.location.places.PlaceLikelihood in project Android-ReactiveLocation by mcharmas.
the class PlacesActivity method onLocationPermissionGranted.
@Override
protected void onLocationPermissionGranted() {
compositeDisposable = new CompositeDisposable();
compositeDisposable.add(reactiveLocationProvider.getCurrentPlace(null).subscribe(new Consumer<PlaceLikelihoodBuffer>() {
@Override
public void accept(PlaceLikelihoodBuffer buffer) {
PlaceLikelihood likelihood = buffer.get(0);
if (likelihood != null) {
currentPlaceView.setText(likelihood.getPlace().getName());
}
buffer.release();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("PlacesActivity", "Error in observable", throwable);
}
}));
Observable<String> queryObservable = RxTextView.textChanges(queryView).map(new Function<CharSequence, String>() {
@Override
public String apply(CharSequence charSequence) {
return charSequence.toString();
}
}).debounce(1, TimeUnit.SECONDS).filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return !TextUtils.isEmpty(s);
}
});
Observable<Location> lastKnownLocationObservable = reactiveLocationProvider.getLastKnownLocation();
Observable<AutocompletePredictionBuffer> suggestionsObservable = Observable.combineLatest(queryObservable, lastKnownLocationObservable, new BiFunction<String, Location, QueryWithCurrentLocation>() {
@Override
public QueryWithCurrentLocation apply(String query, Location currentLocation) {
return new QueryWithCurrentLocation(query, currentLocation);
}
}).flatMap(new Function<QueryWithCurrentLocation, Observable<AutocompletePredictionBuffer>>() {
@Override
public Observable<AutocompletePredictionBuffer> apply(QueryWithCurrentLocation q) {
if (q.location == null)
return Observable.empty();
double latitude = q.location.getLatitude();
double longitude = q.location.getLongitude();
LatLngBounds bounds = new LatLngBounds(new LatLng(latitude - 0.05, longitude - 0.05), new LatLng(latitude + 0.05, longitude + 0.05));
return reactiveLocationProvider.getPlaceAutocompletePredictions(q.query, bounds, null);
}
});
compositeDisposable.add(suggestionsObservable.subscribe(new Consumer<AutocompletePredictionBuffer>() {
@Override
public void accept(AutocompletePredictionBuffer buffer) {
List<AutocompleteInfo> infos = new ArrayList<>();
for (AutocompletePrediction prediction : buffer) {
infos.add(new AutocompleteInfo(prediction.getFullText(null).toString(), prediction.getPlaceId()));
}
buffer.release();
placeSuggestionsList.setAdapter(new ArrayAdapter<>(PlacesActivity.this, android.R.layout.simple_list_item_1, infos));
}
}));
}
Aggregations