Home How can I create observable from Location in location sevice
Reply: 0

How can I create observable from Location in location sevice

Alexander
1#
Alexander Published in 2018-01-12 23:21:54Z

I'm trying to create a service which will provide location listener as observable. Here is what I've done so far. This is inside the service:

...
var locations = Location("")

init {
    locations.latitude = 0.toDouble()
    locations.longitude = 0.toDouble()
}

private val locationSubject: Subject<Location> = BehaviorSubject.createDefault(locations).toSerialized();

fun asObservable(): Observable<Location> {
    return locationSubject
}
 ....
override fun onLocationChanged(p0:Location?){
    locationSubject.onNext(p0!!)
}

And the code bellow is from activity. So I want to receive continues updates from the service rather then getting location only when code is executed.

disposable.addAll(
    locationService.asObservable()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe{
             Toast.makeText(activity,"lat:"+it.latitude,Toast.LENGTH_LONG).show()
            }
    )

Thanks!!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300708 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO