Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.
Merged
2 changes: 2 additions & 0 deletions demo/app/app-root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<Frame defaultPage="main-page">
</Frame>
2 changes: 1 addition & 1 deletion demo/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import * as application from 'tns-core-modules/application';
application.start({ moduleName: "main-page" });
application.run({ moduleName: "app-root" });
8 changes: 8 additions & 0 deletions demo/app/tests/mock-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ var MockLocationManager = (function () {
return _this._requestSingleUpdate(_this.delegate, _this);
}, 500);
};
MockLocationManager.prototype.requestLocation = function () {
var _this = this;
this.removeUpdates(null);
MockLocationManager.intervalId = setTimeout(function () {
// this.delegate is the location listener
return _this._requestSingleUpdate(_this.delegate, _this);
}, 500);
};
MockLocationManager.prototype._requestSingleUpdate = function (locListener, instance) {
var newLocation = {
coordinate: {
Expand Down
34 changes: 28 additions & 6 deletions src/geolocation.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ function errorHandler(errData: UnhandledErrorEventData) {
}
}

function getVersionMaj () {
return parseInt(Platform.device.osVersion.split(".")[0]);
}

// options - desiredAccuracy, updateDistance, minimumUpdateTime, maximumAge, timeout
export function getCurrentLocation(options: Options): Promise<Location> {
return new Promise(function (resolve, reject) {
Expand All @@ -164,6 +168,7 @@ export function getCurrentLocation(options: Options): Promise<Location> {
} else {
let timerId;
let locListener;
let initLocation;

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

let successCallback = function (location: Location) {
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;
if (getVersionMaj() < 9) {
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;
}

if (options.desiredAccuracy !== Accuracy.any && !initLocation) {
// regardless of desired accuracy ios returns first location as quick as possible even if not as accurate as requested
initLocation = location;
return;
}
}

stopTimerAndMonitor(locListener.id);
resolve(location);
};

locListener = LocationListenerImpl.initWithLocationError(successCallback);
locListener = LocationListenerImpl.initWithLocationError(successCallback, reject);
try {
LocationMonitor.startLocationMonitoring(options, locListener);
if (getVersionMaj() >= 9) {
LocationMonitor.requestLocation(options, locListener);
} else {
LocationMonitor.startLocationMonitoring(options, locListener);
}
} catch (e) {
stopTimerAndMonitor(locListener.id);
reject(e);
Expand Down Expand Up @@ -321,6 +338,11 @@ export class LocationMonitor {
return null;
}

static requestLocation(options: Options, locListener: any): void {
let iosLocManager = getIOSLocationManager(locListener, options);
iosLocManager.requestLocation();
}

static startLocationMonitoring(options: Options, locListener: any): void {
let iosLocManager = getIOSLocationManager(locListener, options);
iosLocManager.startUpdatingLocation();
Expand All @@ -342,7 +364,7 @@ export class LocationMonitor {
iosLocManager.distanceFilter = options ? options.updateDistance : minRangeUpdate;
locationManagers[locListener.id] = iosLocManager;
locationListeners[locListener.id] = locListener;
if (parseInt(Platform.device.osVersion.split(".")[0]) >= 9) {
if (getVersionMaj() >= 9) {
iosLocManager.allowsBackgroundLocationUpdates =
options && options.iosAllowsBackgroundLocationUpdates != null ?
options.iosAllowsBackgroundLocationUpdates : false;
Expand Down