Search in sources :

Example 1 with AddressToStringFunc

use of pl.charmas.android.reactivelocation2.sample.utils.AddressToStringFunc in project Android-ReactiveLocation by mcharmas.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
    updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
    addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
    currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
    locationProvider = new ReactiveLocationProvider(getApplicationContext(), ReactiveLocationProviderConfiguration.builder().setRetryOnConnectionSuspended(true).build());
    lastKnownLocationObservable = locationProvider.getLastKnownLocation().observeOn(AndroidSchedulers.mainThread());
    final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
    locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(// Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
    true).build()).doOnNext(new Consumer<LocationSettingsResult>() {

        @Override
        public void accept(LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException th) {
                    Log.e("MainActivity", "Error opening settings activity.", th);
                }
            }
        }
    }).flatMap(new Function<LocationSettingsResult, Observable<Location>>() {

        @Override
        public Observable<Location> apply(LocationSettingsResult locationSettingsResult) {
            return locationProvider.getUpdatedLocation(locationRequest);
        }
    }).observeOn(AndroidSchedulers.mainThread());
    addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Function<Location, Observable<List<Address>>>() {

        @Override
        public Observable<List<Address>> apply(Location location) {
            return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
        }
    }).map(new Function<List<Address>, Address>() {

        @Override
        public Address apply(List<Address> addresses) {
            return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
        }
    }).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    activityObservable = locationProvider.getDetectedActivity(50).observeOn(AndroidSchedulers.mainThread());
}
Also used : Status(com.google.android.gms.common.api.Status) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Address(android.location.Address) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) Consumer(io.reactivex.functions.Consumer) List(java.util.List) ReactiveLocationProvider(pl.charmas.android.reactivelocation2.ReactiveLocationProvider) IntentSender(android.content.IntentSender) AddressToStringFunc(pl.charmas.android.reactivelocation2.sample.utils.AddressToStringFunc) Location(android.location.Location)

Aggregations

IntentSender (android.content.IntentSender)1 Address (android.location.Address)1 Location (android.location.Location)1 Status (com.google.android.gms.common.api.Status)1 LocationRequest (com.google.android.gms.location.LocationRequest)1 LocationSettingsResult (com.google.android.gms.location.LocationSettingsResult)1 Observable (io.reactivex.Observable)1 Consumer (io.reactivex.functions.Consumer)1 Function (io.reactivex.functions.Function)1 List (java.util.List)1 ReactiveLocationProvider (pl.charmas.android.reactivelocation2.ReactiveLocationProvider)1 AddressToStringFunc (pl.charmas.android.reactivelocation2.sample.utils.AddressToStringFunc)1