Skip to content

Commit

Permalink
Geolocation Accuracy and Cached Location Bug
Browse files Browse the repository at this point in the history
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 #6198

Differential Revision: D3023786

Pulled By: nicklockwood

fb-gh-sync-id: 869c0e34252470275bf99cf0e5861747247f1158
shipit-source-id: 869c0e34252470275bf99cf0e5861747247f1158
  • Loading branch information
realaboo authored and Facebook Github Bot 6 committed Mar 8, 2016
1 parent 102a31c commit 49b2805
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (dispatch_queue_t)methodQueue

#pragma mark - Private API

- (void)beginLocationUpdates
- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy
{
if (!_locationManager) {
_locationManager = [CLLocationManager new];
Expand All @@ -147,6 +147,7 @@ - (void)beginLocationUpdates
[_locationManager requestWhenInUseAuthorization];
}

_locationManager.desiredAccuracy = desiredAccuracy;
// Start observing location
[_locationManager startUpdatingLocation];
}
Expand Down Expand Up @@ -178,8 +179,7 @@ - (void)timeout:(NSTimer *)timer
_observerOptions.accuracy = MIN(_observerOptions.accuracy, request.options.accuracy);
}

_locationManager.desiredAccuracy = _observerOptions.accuracy;
[self beginLocationUpdates];
[self beginLocationUpdatesWithDesiredAccuracy:_observerOptions.accuracy];
_observingLocation = YES;
}

Expand Down Expand Up @@ -225,8 +225,8 @@ - (void)timeout:(NSTimer *)timer

// 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]);
Expand All @@ -249,8 +249,11 @@ - (void)timeout:(NSTimer *)timer
[_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
Expand Down Expand Up @@ -286,7 +289,7 @@ - (void)locationManager:(CLLocationManager *)manager
}
[_pendingRequests removeAllObjects];

// Stop updating if not not observing
// Stop updating if not observing
if (!_observingLocation) {
[_locationManager stopUpdatingLocation];
}
Expand Down

0 comments on commit 49b2805

Please sign in to comment.