Search in sources :

Example 1 with PlaceLikelihood

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));
        }
    }));
}
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 PlaceLikelihood

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");
        }
    });
}
Also used : PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) SuppressLint(android.annotation.SuppressLint) LatLng(com.google.android.gms.maps.model.LatLng) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) GenLocation(garbagecollectors.com.unipool.GenLocation) Place(com.google.android.gms.location.places.Place)

Example 3 with PlaceLikelihood

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;
    }
}
Also used : Status(com.google.android.gms.common.api.Status) PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) WritableMap(com.facebook.react.bridge.WritableMap) WritableArray(com.facebook.react.bridge.WritableArray) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 4 with PlaceLikelihood

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));
        }
    }));
}
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)

Aggregations

PlaceLikelihood (com.google.android.gms.location.places.PlaceLikelihood)4 PlaceLikelihoodBuffer (com.google.android.gms.location.places.PlaceLikelihoodBuffer)4 LatLng (com.google.android.gms.maps.model.LatLng)3 Location (android.location.Location)2 AutocompletePrediction (com.google.android.gms.location.places.AutocompletePrediction)2 AutocompletePredictionBuffer (com.google.android.gms.location.places.AutocompletePredictionBuffer)2 LatLngBounds (com.google.android.gms.maps.model.LatLngBounds)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 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 Place (com.google.android.gms.location.places.Place)1 GenLocation (garbagecollectors.com.unipool.GenLocation)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