Skip to content

Instantly share code, notes, and snippets.

@Kudo
Last active November 26, 2015 17:13
Show Gist options
  • Save Kudo/c29106824d4ed73ccf9a to your computer and use it in GitHub Desktop.
Save Kudo/c29106824d4ed73ccf9a to your computer and use it in GitHub Desktop.
RxRNBridge with Android-ReactiveLocation
public class LocationManager extends ReactContextBaseJavaModule {
public static final String TAG = "LocationManager";
private static final int LOCATION_TIMEOUT_IN_SECONDS = 10;
private static final int LOCATION_UPDATE_INTERVAL = 1000;
public LocationManager(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return TAG;
}
@ReactMethodObservable
public Observable<WritableMap> getLocation() {
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getReactApplicationContext());
Observable<Location> observableFromCache = locationProvider.getLastKnownLocation();
final LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS))
.setInterval(LOCATION_UPDATE_INTERVAL)
.setNumUpdates(1);
Observable<Location> observableFromRealRequest = locationProvider.getUpdatedLocation(locationRequest)
.timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
.first();
return Observable.<Location>concat(observableFromCache, observableFromRealRequest)
.first()
.flatMap(new Func1<Location, Observable<WritableMap>>() {
@Override
public Observable<WritableMap> call(Location location) {
if (location == null) { return Observable.just(null); }
WritableMap retObject = Arguments.createMap();
retObject.putDouble("latitude", location.getLatitude());
retObject.putDouble("longitude", location.getLongitude());
return Observable.just(retObject);
}
});
}
}
"use strict";
var LocationManager = require("NativeModules").LocationManager;
var LocationModule = {
getLocation: function(): Promise {
return new Promise((resolve, reject) => {
LocationManager.getLocation(
(error) => {
reject(error);
}, (releases) => {
resolve(releases);
});
});
}
};
module.exports = LocationModule;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment