use of com.google.android.gms.location.places.PlaceLikelihoodBuffer 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));
}
}));
}
Aggregations