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 facebook#6198

Differential Revision: D3023786

Pulled By: nicklockwood

fb-gh-sync-id: 869c0e34252470275bf99cf0e5861747247f1158
shipit-source-id: 869c0e34252470275bf99cf0e5861747247f1158
  • Loading branch information
realaboo authored and machard committed Mar 22, 2016
1 parent 7b2fa56 commit 8b5b6a6
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 @@ -124,7 +124,7 @@ - (dispatch_queue_t)methodQueue

#pragma mark - Private API

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

_locationManager.desiredAccuracy = desiredAccuracy;
// Start observing location
[_locationManager startUpdatingLocation];
}
Expand Down Expand Up @@ -174,8 +175,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 @@ -221,8 +221,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 @@ -245,8 +245,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 @@ -282,7 +285,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 8b5b6a6

Please sign in to comment.