Geolocation Accuracy and Cached Location Bug

Summary:There are two issues:

1. When calling startObserving or getCurrentPosition for the first time, _locationManager is not initialized and the accuracy value set in options is lost.

2. When using the cached location _lastLocationEvent, current timestamp retrieved by CFAbsoluteTimeGetCurrent() is from 2001, but the timestamp of the location is from 1970. In addition, the comparison between _lastLocationEvent[@"coords"][@"accuracy"] and options.accuracy is incorrect. Less value indicates more accurate location.
Closes https://github.com/facebook/react-native/pull/6198

Differential Revision: D3023786

Pulled By: nicklockwood

fb-gh-sync-id: 869c0e34252470275bf99cf0e5861747247f1158
shipit-source-id: 869c0e34252470275bf99cf0e5861747247f1158
This commit is contained in:
realaboo 2016-03-08 05:16:32 -08:00 committed by Facebook Github Bot 6
parent 102a31c13e
commit 49b2805da2
1 changed files with 11 additions and 8 deletions

View File

@ -130,7 +130,7 @@ RCT_EXPORT_MODULE()
#pragma mark - Private API
- (void)beginLocationUpdates
- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy
{
if (!_locationManager) {
_locationManager = [CLLocationManager new];
@ -147,6 +147,7 @@ RCT_EXPORT_MODULE()
[_locationManager requestWhenInUseAuthorization];
}
_locationManager.desiredAccuracy = desiredAccuracy;
// Start observing location
[_locationManager startUpdatingLocation];
}
@ -178,8 +179,7 @@ RCT_EXPORT_METHOD(startObserving:(RCTLocationOptions)options)
_observerOptions.accuracy = MIN(_observerOptions.accuracy, request.options.accuracy);
}
_locationManager.desiredAccuracy = _observerOptions.accuracy;
[self beginLocationUpdates];
[self beginLocationUpdatesWithDesiredAccuracy:_observerOptions.accuracy];
_observingLocation = YES;
}
@ -225,8 +225,8 @@ RCT_EXPORT_METHOD(getCurrentPosition:(RCTLocationOptions)options
// Check if previous recorded location exists and is good enough
if (_lastLocationEvent &&
CFAbsoluteTimeGetCurrent() - [RCTConvert NSTimeInterval:_lastLocationEvent[@"timestamp"]] < options.maximumAge &&
[_lastLocationEvent[@"coords"][@"accuracy"] doubleValue] >= options.accuracy) {
[NSDate date].timeIntervalSince1970 - [RCTConvert NSTimeInterval:_lastLocationEvent[@"timestamp"]] < options.maximumAge &&
[_lastLocationEvent[@"coords"][@"accuracy"] doubleValue] <= options.accuracy) {
// Call success block with most recent known location
successBlock(@[_lastLocationEvent]);
@ -249,8 +249,11 @@ RCT_EXPORT_METHOD(getCurrentPosition:(RCTLocationOptions)options
[_pendingRequests addObject:request];
// Configure location manager and begin updating location
_locationManager.desiredAccuracy = MIN(_locationManager.desiredAccuracy, options.accuracy);
[self beginLocationUpdates];
CLLocationAccuracy accuracy = options.accuracy;
if (_locationManager) {
accuracy = MIN(_locationManager.desiredAccuracy, accuracy);
}
[self beginLocationUpdatesWithDesiredAccuracy:accuracy];
}
#pragma mark - CLLocationManagerDelegate
@ -286,7 +289,7 @@ RCT_EXPORT_METHOD(getCurrentPosition:(RCTLocationOptions)options
}
[_pendingRequests removeAllObjects];
// Stop updating if not not observing
// Stop updating if not observing
if (!_observingLocation) {
[_locationManager stopUpdatingLocation];
}