Skip to content

Commit

Permalink
[Geolocation] Expose option for distance filtering on location updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherdro committed Jan 30, 2016
1 parent c1aed7b commit 10affb9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Libraries/Geolocation/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type GeoOptions = {
timeout: number;
maximumAge: number;
enableHighAccuracy: bool;
distanceFilter: number;
}

/**
Expand Down Expand Up @@ -68,7 +69,7 @@ var Geolocation = {

/*
* Invokes the success callback whenever the location changes. Supported
* options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool)
* options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool), distanceFilter(m)
*/
watchPosition: function(success: Function, error?: Function, options?: GeoOptions): number {
if (!updatesEnabled) {
Expand Down
10 changes: 8 additions & 2 deletions Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {
double timeout;
double maximumAge;
double accuracy;
double distanceFilter;
} RCTLocationOptions;

@implementation RCTConvert (RCTLocationOptions)

+ (RCTLocationOptions)RCTLocationOptions:(id)json
{
NSDictionary<NSString *, id> *options = [RCTConvert NSDictionary:json];

double accuracy = options[@"distanceFilter"] == NULL ? RCT_DEFAULT_LOCATION_ACCURACY
: [RCTConvert double:options[@"distanceFilter"]] ?: kCLDistanceFilterNone;

return (RCTLocationOptions){
.timeout = [RCTConvert NSTimeInterval:options[@"timeout"]] ?: INFINITY,
.maximumAge = [RCTConvert NSTimeInterval:options[@"maximumAge"]] ?: INFINITY,
.accuracy = [RCTConvert BOOL:options[@"enableHighAccuracy"]] ? kCLLocationAccuracyBest : RCT_DEFAULT_LOCATION_ACCURACY
.accuracy = [RCTConvert BOOL:options[@"enableHighAccuracy"]] ? kCLLocationAccuracyBest : RCT_DEFAULT_LOCATION_ACCURACY,
.distanceFilter = accuracy
};
}

Expand Down Expand Up @@ -128,7 +134,7 @@ - (void)beginLocationUpdates
{
if (!_locationManager) {
_locationManager = [CLLocationManager new];
_locationManager.distanceFilter = RCT_DEFAULT_LOCATION_ACCURACY;
_locationManager.distanceFilter = _observerOptions.distanceFilter;
_locationManager.delegate = self;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public class LocationModule extends ReactContextBaseJavaModule {

private @Nullable String mWatchedProvider;
private static final float RCT_DEFAULT_LOCATION_ACCURACY = 100;

private final LocationListener mLocationListener = new LocationListener() {
@Override
Expand Down Expand Up @@ -73,11 +74,13 @@ private static class LocationOptions {
private final long timeout;
private final double maximumAge;
private final boolean highAccuracy;
private final float distanceFilter;

private LocationOptions(long timeout, double maximumAge, boolean highAccuracy) {
private LocationOptions(long timeout, double maximumAge, boolean highAccuracy, float distanceFilter) {
this.timeout = timeout;
this.maximumAge = maximumAge;
this.highAccuracy = highAccuracy;
this.distanceFilter = distanceFilter;
}

private static LocationOptions fromReactMap(ReadableMap map) {
Expand All @@ -88,8 +91,10 @@ private static LocationOptions fromReactMap(ReadableMap map) {
map.hasKey("maximumAge") ? map.getDouble("maximumAge") : Double.POSITIVE_INFINITY;
boolean highAccuracy =
map.hasKey("enableHighAccuracy") && map.getBoolean("enableHighAccuracy");
float distanceFilter =
map.hasKey("distanceFilter") ? (float) map.getDouble("distanceFilter") : RCT_DEFAULT_LOCATION_ACCURACY;

return new LocationOptions(timeout, maximumAge, highAccuracy);
return new LocationOptions(timeout, maximumAge, highAccuracy, distanceFilter);
}
}

Expand Down Expand Up @@ -151,7 +156,7 @@ public void startObserving(ReadableMap options) {
}
if (!provider.equals(mWatchedProvider)) {
locationManager.removeUpdates(mLocationListener);
locationManager.requestLocationUpdates(provider, 1000, 0, mLocationListener);
locationManager.requestLocationUpdates(provider, 1000, locationOptions.distanceFilter, mLocationListener);
}
mWatchedProvider = provider;
} catch (SecurityException e) {
Expand Down

0 comments on commit 10affb9

Please sign in to comment.