From 6a228784e7f37f3c58c0259ae0ac896b6c99552e Mon Sep 17 00:00:00 2001 From: Stoyan Stratev Date: Fri, 15 Dec 2017 09:41:21 +0200 Subject: [PATCH 1/2] fix iOS returning old location when maximumAge is set First, get a timestamp of when the app is started and prevent locations older than that from being returned. After a recent (after app started) location is aquired, clear the app start timestamp and continue checking the maximumAge to maintain location accuracy. Remove "location too old" check when timeout is set - proper logic is just to keep waiting for a recent location until timeout expires. --- src/geolocation.ios.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/geolocation.ios.ts b/src/geolocation.ios.ts index 4c3ed43..ce225dd 100644 --- a/src/geolocation.ios.ts +++ b/src/geolocation.ios.ts @@ -15,6 +15,7 @@ import * as Platform from "platform"; const locationManagers = {}; const locationListeners = {}; +let dateStarted = new Date().valueOf(); let watchId = 0; class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate { @@ -182,16 +183,18 @@ export function getCurrentLocation(options: Options): Promise { }; let successCallback = function (location: Location) { - stopTimerAndMonitor(locListener.id); if (typeof options.maximumAge === "number") { - if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) { - resolve(location); - } else { - reject(new Error("New location is older than requested maximum age!")); + if ((dateStarted && location.timestamp.valueOf() < dateStarted) || + (location.timestamp.valueOf() + options.maximumAge < new Date().valueOf())) { + // returned location is too old, but we still have some time before the timeout so maybe wait a bit? + return; } - } else { - resolve(location); } + + stopTimerAndMonitor(locListener.id); + // clear date value that was set on startup as we don't need it anymore + dateStarted = 0; + resolve(location); }; locListener = LocationListenerImpl.initWithLocationError(successCallback); From ceb6eb30d38bfd66da64cee1e88eeecf73d1df8f Mon Sep 17 00:00:00 2001 From: Stoyan Stratev Date: Fri, 15 Dec 2017 13:18:50 +0200 Subject: [PATCH 2/2] remove location logic applied at app startup --- src/geolocation.ios.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/geolocation.ios.ts b/src/geolocation.ios.ts index ce225dd..2e4a512 100644 --- a/src/geolocation.ios.ts +++ b/src/geolocation.ios.ts @@ -15,7 +15,6 @@ import * as Platform from "platform"; const locationManagers = {}; const locationListeners = {}; -let dateStarted = new Date().valueOf(); let watchId = 0; class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate { @@ -183,17 +182,12 @@ export function getCurrentLocation(options: Options): Promise { }; let successCallback = function (location: Location) { - if (typeof options.maximumAge === "number") { - if ((dateStarted && location.timestamp.valueOf() < dateStarted) || - (location.timestamp.valueOf() + options.maximumAge < new Date().valueOf())) { - // returned location is too old, but we still have some time before the timeout so maybe wait a bit? - return; - } + if (typeof options.maximumAge === "number" && location.timestamp.valueOf() + options.maximumAge < new Date().valueOf()) { + // returned location is too old, but we still have some time before the timeout so maybe wait a bit? + return; } stopTimerAndMonitor(locListener.id); - // clear date value that was set on startup as we don't need it anymore - dateStarted = 0; resolve(location); };