From 8df746783332ea1a792cc9e5b504331df574660e Mon Sep 17 00:00:00 2001 From: realaboo Date: Sun, 28 Feb 2016 17:53:51 +0800 Subject: [PATCH] Fix cached location usage. Fix _locationManager initialization. --- Libraries/Geolocation/RCTLocationObserver.m | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Libraries/Geolocation/RCTLocationObserver.m b/Libraries/Geolocation/RCTLocationObserver.m index 080cc9f4eab357..c2ac267d318cfd 100644 --- a/Libraries/Geolocation/RCTLocationObserver.m +++ b/Libraries/Geolocation/RCTLocationObserver.m @@ -130,7 +130,7 @@ - (dispatch_queue_t)methodQueue #pragma mark - Private API -- (void)beginLocationUpdates +- (void)beginLocationUpdatesInDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy { if (!_locationManager) { _locationManager = [CLLocationManager new]; @@ -147,6 +147,7 @@ - (void)beginLocationUpdates [_locationManager requestWhenInUseAuthorization]; } + _locationManager.desiredAccuracy = desiredAccuracy; // Start observing location [_locationManager startUpdatingLocation]; } @@ -178,8 +179,7 @@ - (void)timeout:(NSTimer *)timer _observerOptions.accuracy = MIN(_observerOptions.accuracy, request.options.accuracy); } - _locationManager.desiredAccuracy = _observerOptions.accuracy; - [self beginLocationUpdates]; + [self beginLocationUpdatesInDesiredAccuracy:_observerOptions.accuracy]; _observingLocation = YES; } @@ -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]); @@ -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]; + if (_locationManager) { + [self beginLocationUpdatesInDesiredAccuracy:MIN(_locationManager.desiredAccuracy, options.accuracy)]; + } else { + [self beginLocationUpdatesInDesiredAccuracy:options.accuracy]; + } } #pragma mark - CLLocationManagerDelegate