2015-12-19 14 views

risposta

8

È possibile creare oggetti osservabili o Oggetto utilizzando LocationListener e quando si riceve la prenotazione nello onLocationChanged, è sufficiente chiamare suNext con l'oggetto posizione.

public final class LocationProvider implements onLocationChanged { 
    private final PublishSubject<Location> latestLocation = PublishSubject.create(); 
    //... 
    @Override 
    public void onLocationChanged(Location location) { 
     latestLocation.onNext(location); 
    } 
} 

Quindi è possibile iscriversi ad esso in una classe che necessita di posizione.

ci sono anche alcune librerie open source che è possibile utilizzare: Android-ReactiveLocation e cgeo

Inoltre, vedere Observable-based API and unsubscribe issue

Spero che questo aiuti!

0

Che ne dici di utilizzare questa fantastica libreria? https://github.com/patloew/RxLocation

aggiungere questo al build.gradle

compile 'com.patloew.rxlocation:rxlocation:1.0.4' 

è possibile utilizzare questo frammento dalla libreria di cui sopra per la sottoscrizione sui cambiamenti di posizione, credo che questo sia il modo più semplice

// Create one instance and share it 
RxLocation rxLocation = new RxLocation(context); 

LocationRequest locationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(5000); 

rxLocation.location().updates(locationRequest) 
     .flatMap(location -> rxLocation.geocoding().fromLocation(location).toObservable()) 
     .subscribe(address -> { 
      /* do something */ 
     }); 
Problemi correlati