use of com.google.android.gms.location.places.AutocompletePrediction 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.AutocompletePrediction 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));
}
}));
}
use of com.google.android.gms.location.places.AutocompletePrediction in project react-native-google-places by tolu360.
the class RNGooglePlacesModule method getAutocompletePredictions.
@ReactMethod
public void getAutocompletePredictions(String query, ReadableMap options, final Promise promise) {
String type = options.getString("type");
String country = options.getString("country");
country = country.isEmpty() ? null : country;
double latitude = options.getDouble("latitude");
double longitude = options.getDouble("longitude");
double radius = options.getDouble("radius");
LatLng center = new LatLng(latitude, longitude);
LatLngBounds bounds = null;
if (latitude != 0 && longitude != 0 && radius != 0) {
bounds = this.getLatLngBounds(center, radius);
}
PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query, bounds, getFilterType(type, country));
AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);
final Status status = autocompletePredictions.getStatus();
if (status.isSuccess()) {
if (autocompletePredictions.getCount() == 0) {
WritableArray emptyResult = Arguments.createArray();
autocompletePredictions.release();
promise.resolve(emptyResult);
return;
}
WritableArray predictionsList = Arguments.createArray();
for (AutocompletePrediction prediction : autocompletePredictions) {
WritableMap map = Arguments.createMap();
map.putString("fullText", prediction.getFullText(null).toString());
map.putString("primaryText", prediction.getPrimaryText(null).toString());
map.putString("secondaryText", prediction.getSecondaryText(null).toString());
map.putString("placeID", prediction.getPlaceId().toString());
if (prediction.getPlaceTypes() != null) {
List<String> types = new ArrayList<>();
for (Integer placeType : prediction.getPlaceTypes()) {
types.add(findPlaceTypeLabelByPlaceTypeId(placeType));
}
map.putArray("types", Arguments.fromArray(types.toArray(new String[0])));
}
predictionsList.pushMap(map);
}
// Release the buffer now that all data has been copied.
autocompletePredictions.release();
promise.resolve(predictionsList);
} else {
Log.i(TAG, "Error making autocomplete prediction API call: " + status.toString());
autocompletePredictions.release();
promise.reject("E_AUTOCOMPLETE_ERROR", new Error("Error making autocomplete prediction API call: " + status.toString()));
return;
}
}
Aggregations