Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 1c9b8d1

Browse files
Merge pull request #200 from AcornIT/iosaccuracy
fix ios accuracy
2 parents 9f37f17 + 8e706ac commit 1c9b8d1

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

demo/app/tests/mock-ios.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ var MockLocationManager = (function () {
1919
return _this._requestSingleUpdate(_this.delegate, _this);
2020
}, 500);
2121
};
22+
MockLocationManager.prototype.requestLocation = function () {
23+
var _this = this;
24+
this.removeUpdates(null);
25+
MockLocationManager.intervalId = setTimeout(function () {
26+
// this.delegate is the location listener
27+
return _this._requestSingleUpdate(_this.delegate, _this);
28+
}, 500);
29+
};
2230
MockLocationManager.prototype._requestSingleUpdate = function (locListener, instance) {
2331
var newLocation = {
2432
coordinate: {

src/geolocation.ios.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ function errorHandler(errData: UnhandledErrorEventData) {
140140
}
141141
}
142142

143+
function getVersionMaj () {
144+
return parseInt(Platform.device.osVersion.split(".")[0]);
145+
}
146+
143147
// options - desiredAccuracy, updateDistance, minimumUpdateTime, maximumAge, timeout
144148
export function getCurrentLocation(options: Options): Promise<Location> {
145149
return new Promise(function (resolve, reject) {
@@ -164,6 +168,7 @@ export function getCurrentLocation(options: Options): Promise<Location> {
164168
} else {
165169
let timerId;
166170
let locListener;
171+
let initLocation;
167172

168173
let stopTimerAndMonitor = function (locListenerId) {
169174
if (timerId !== undefined) {
@@ -174,18 +179,30 @@ export function getCurrentLocation(options: Options): Promise<Location> {
174179
};
175180

176181
let successCallback = function (location: Location) {
177-
if (typeof options.maximumAge === "number" && location.timestamp.valueOf() + options.maximumAge < new Date().valueOf()) {
178-
// returned location is too old, but we still have some time before the timeout so maybe wait a bit?
179-
return;
182+
if (getVersionMaj() < 9) {
183+
if (typeof options.maximumAge === "number" && location.timestamp.valueOf() + options.maximumAge < new Date().valueOf()) {
184+
// returned location is too old, but we still have some time before the timeout so maybe wait a bit?
185+
return;
186+
}
187+
188+
if (options.desiredAccuracy !== Accuracy.any && !initLocation) {
189+
// regardless of desired accuracy ios returns first location as quick as possible even if not as accurate as requested
190+
initLocation = location;
191+
return;
192+
}
180193
}
181194

182195
stopTimerAndMonitor(locListener.id);
183196
resolve(location);
184197
};
185198

186-
locListener = LocationListenerImpl.initWithLocationError(successCallback);
199+
locListener = LocationListenerImpl.initWithLocationError(successCallback, reject);
187200
try {
188-
LocationMonitor.startLocationMonitoring(options, locListener);
201+
if (getVersionMaj() >= 9) {
202+
LocationMonitor.requestLocation(options, locListener);
203+
} else {
204+
LocationMonitor.startLocationMonitoring(options, locListener);
205+
}
189206
} catch (e) {
190207
stopTimerAndMonitor(locListener.id);
191208
reject(e);
@@ -321,6 +338,11 @@ export class LocationMonitor {
321338
return null;
322339
}
323340

341+
static requestLocation(options: Options, locListener: any): void {
342+
let iosLocManager = getIOSLocationManager(locListener, options);
343+
iosLocManager.requestLocation();
344+
}
345+
324346
static startLocationMonitoring(options: Options, locListener: any): void {
325347
let iosLocManager = getIOSLocationManager(locListener, options);
326348
iosLocManager.startUpdatingLocation();
@@ -342,7 +364,7 @@ export class LocationMonitor {
342364
iosLocManager.distanceFilter = options ? options.updateDistance : minRangeUpdate;
343365
locationManagers[locListener.id] = iosLocManager;
344366
locationListeners[locListener.id] = locListener;
345-
if (parseInt(Platform.device.osVersion.split(".")[0]) >= 9) {
367+
if (getVersionMaj() >= 9) {
346368
iosLocManager.allowsBackgroundLocationUpdates =
347369
options && options.iosAllowsBackgroundLocationUpdates != null ?
348370
options.iosAllowsBackgroundLocationUpdates : false;

0 commit comments

Comments
 (0)