Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. public class LocationManager extends ReactContextBaseJavaModule {
  2. public static final String TAG = "LocationManager";
  3. private static final int LOCATION_TIMEOUT_IN_SECONDS = 10;
  4. private static final int LOCATION_UPDATE_INTERVAL = 1000;
  5.  
  6. public LocationManager(ReactApplicationContext reactContext) {
  7. super(reactContext);
  8. }
  9.  
  10. @Override
  11. public String getName() {
  12. return TAG;
  13. }
  14.  
  15. @ReactMethodObservable
  16. public Observable<WritableMap> getLocation() {
  17. ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getReactApplicationContext());
  18.  
  19. Observable<Location> observableFromCache = locationProvider.getLastKnownLocation();
  20.  
  21. final LocationRequest locationRequest = LocationRequest.create()
  22. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
  23. .setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS))
  24. .setInterval(LOCATION_UPDATE_INTERVAL)
  25. .setNumUpdates(1);
  26. Observable<Location> observableFromRealRequest = locationProvider.getUpdatedLocation(locationRequest)
  27. .timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
  28. .first();
  29.  
  30. return Observable.<Location>concat(observableFromCache, observableFromRealRequest)
  31. .first()
  32. .flatMap(new Func1<Location, Observable<WritableMap>>() {
  33. @Override
  34. public Observable<WritableMap> call(Location location) {
  35. if (location == null) { return Observable.just(null); }
  36. WritableMap retObject = Arguments.createMap();
  37. retObject.putDouble("latitude", location.getLatitude());
  38. retObject.putDouble("longitude", location.getLongitude());
  39. return Observable.just(retObject);
  40. }
  41. });
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement