florent37 / RxGps

Finding current location cannot be easier on Android !
Apache License 2.0
300 stars 42 forks source link

Location timeout #10

Open prajapatiravi257 opened 6 years ago

prajapatiravi257 commented 6 years ago

How to add a timeout in observable of lastlocation() call if it's taking to much time to get location

bluetoothfx commented 6 years ago

I am not sure if it will work or not. But please let me know if it works. Thanks.

  RxLocation rxLocation = new RxLocation(this);
  rxLocation.setDefaultTimeout(25,TimeUnit.SECONDS);
  final RxGps rxGps = new RxGps(rxLocation,this);

rxGps.lastLocation()
                .doOnSubscribe(this::addDisposable)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(location -> {
                    locationText.setText(location.getLatitude() + ", " + location.getLongitude() + ", " + location.getProvider());
                }, throwable -> {
                    if (throwable instanceof RxGps.PermissionException) {
                        displayError(throwable.getMessage());
                    } else if (throwable instanceof RxGps.PlayServicesNotAvailableException) {
                        displayError(throwable.getMessage());
                    }
                });
prajapatiravi257 commented 6 years ago

Sure, I will give it a try. Thanks BTW

prajapatiravi257 commented 6 years ago

It works but is there any which can inform timeout occurred while fetching location

bluetoothfx commented 6 years ago

I did not find any but you may add timeout function and then check exactly after that time if Latitude and Longitude is null or not. I hope this may work.

rxGps.lastLocation() .timeout(10, TimeUnit.SECOND) .doOnSubscribe(this::addDisposable) ........................................................................ ...............