Search in sources :

Example 1 with AutocompletePrediction

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));
        }
    }));
}
Also used : PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) AutocompletePredictionBuffer(com.google.android.gms.location.places.AutocompletePredictionBuffer) ArrayList(java.util.ArrayList) LatLng(com.google.android.gms.maps.model.LatLng) Func2(rx.functions.Func2) Action1(rx.functions.Action1) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Observable(rx.Observable) CompositeSubscription(rx.subscriptions.CompositeSubscription) AutocompletePrediction(com.google.android.gms.location.places.AutocompletePrediction) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) Location(android.location.Location)

Example 2 with AutocompletePrediction

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));
        }
    }));
}
Also used : PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) AutocompletePredictionBuffer(com.google.android.gms.location.places.AutocompletePredictionBuffer) ArrayList(java.util.ArrayList) Consumer(io.reactivex.functions.Consumer) LatLng(com.google.android.gms.maps.model.LatLng) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Observable(io.reactivex.Observable) BiFunction(io.reactivex.functions.BiFunction) AutocompletePrediction(com.google.android.gms.location.places.AutocompletePrediction) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Location(android.location.Location)

Example 3 with AutocompletePrediction

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;
    }
}
Also used : Status(com.google.android.gms.common.api.Status) WritableMap(com.facebook.react.bridge.WritableMap) WritableArray(com.facebook.react.bridge.WritableArray) AutocompletePredictionBuffer(com.google.android.gms.location.places.AutocompletePredictionBuffer) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) ArrayList(java.util.ArrayList) AutocompletePrediction(com.google.android.gms.location.places.AutocompletePrediction) LatLng(com.google.android.gms.maps.model.LatLng) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

AutocompletePrediction (com.google.android.gms.location.places.AutocompletePrediction)3 AutocompletePredictionBuffer (com.google.android.gms.location.places.AutocompletePredictionBuffer)3 LatLng (com.google.android.gms.maps.model.LatLng)3 LatLngBounds (com.google.android.gms.maps.model.LatLngBounds)3 ArrayList (java.util.ArrayList)3 Location (android.location.Location)2 PlaceLikelihood (com.google.android.gms.location.places.PlaceLikelihood)2 PlaceLikelihoodBuffer (com.google.android.gms.location.places.PlaceLikelihoodBuffer)2 ReactMethod (com.facebook.react.bridge.ReactMethod)1 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 Status (com.google.android.gms.common.api.Status)1 Observable (io.reactivex.Observable)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 BiFunction (io.reactivex.functions.BiFunction)1 Consumer (io.reactivex.functions.Consumer)1 Observable (rx.Observable)1 Action1 (rx.functions.Action1)1 Func2 (rx.functions.Func2)1 CompositeSubscription (rx.subscriptions.CompositeSubscription)1