Fix distanceFilter caching for LocationObserver

Summary:
Allow changing distanceFilter after _locationManager has been initialized.

There is 2 reasons for this PR:
- When calling `getCurrentPosition`, `_observerOptions` is possibly not set properly ( as it is set only in `startObserving`), in this case it would have default values so `distanceFilter` will be 0 in this case ( which can be the right value but we should be more explicit about it, `kCLDistanceFilterNone` or `RCT_DEFAULT_LOCATION_ACCURACY`):
<img width="961" alt="screen shot 2016-04-14 at 8 44 09 pm" src="https://cloud.githubusercontent.com/assets/159813/14551465/6aa8791a-0288-11e6-9c98-1687357f8c2a.png">
- Another issue is that `distanceFilter` is cached so it can't be changed afterwards:

```javascript
let options;
options = {
  enableHighAccuracy: true,
  distanceFilter: 20,
};

this.watchId = this.geolocation.watchPosition(
  () => { },
  () => { },
  options,
);

// => sets distanceFilter to 20

this.geolocation.clearWatch(this.watchId);

options = {
  enableHighAccuracy:
Closes https://github.com/facebook/react-native/pull/6987

Differential Revision: D3258956

fb-gh-sync-id: 00a1d1b29d732a54cdc30e20a7a9a2de3dd9b725
fbshipit-source-id: 00a1d1b29d732a54cdc30e20a7a9a2de3dd9b725
This commit is contained in:
Jean-Richard Lai 2016-05-04 10:33:58 -07:00 committed by Facebook Github Bot 4
parent 041185edfd
commit 2310494f32
1 changed files with 5 additions and 4 deletions

View File

@ -130,11 +130,11 @@ RCT_EXPORT_MODULE()
#pragma mark - Private API
- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy
- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy distanceFilter:(CLLocationDistance)distanceFilter
{
if (!_locationManager) {
_locationManager = [CLLocationManager new];
_locationManager.distanceFilter = _observerOptions.distanceFilter;
_locationManager.delegate = self;
}
@ -147,6 +147,7 @@ RCT_EXPORT_MODULE()
[_locationManager requestWhenInUseAuthorization];
}
_locationManager.distanceFilter = distanceFilter;
_locationManager.desiredAccuracy = desiredAccuracy;
// Start observing location
[_locationManager startUpdatingLocation];
@ -179,7 +180,7 @@ RCT_EXPORT_METHOD(startObserving:(RCTLocationOptions)options)
_observerOptions.accuracy = MIN(_observerOptions.accuracy, request.options.accuracy);
}
[self beginLocationUpdatesWithDesiredAccuracy:_observerOptions.accuracy];
[self beginLocationUpdatesWithDesiredAccuracy:_observerOptions.accuracy distanceFilter:_observerOptions.distanceFilter];
_observingLocation = YES;
}
@ -253,7 +254,7 @@ RCT_EXPORT_METHOD(getCurrentPosition:(RCTLocationOptions)options
if (_locationManager) {
accuracy = MIN(_locationManager.desiredAccuracy, accuracy);
}
[self beginLocationUpdatesWithDesiredAccuracy:accuracy];
[self beginLocationUpdatesWithDesiredAccuracy:accuracy distanceFilter:options.distanceFilter];
}
#pragma mark - CLLocationManagerDelegate