updated location and location tests

This commit is contained in:
Stanimir Karoserov
2014-05-14 19:30:10 +03:00
parent 0242773cae
commit e8d3669d98
7 changed files with 163 additions and 16 deletions

View File

@@ -1,9 +1,12 @@
import types = require("location/location_types");
import appModule = require("application/application");
import common = require("location/location_common");
import merger = require("utils/module_merge");
// merge the exports of the types module with the exports of this file
declare var exports;
require("utils/module_merge").merge(types, exports);
merger.merge(types, exports);
merger.merge(common, exports);
export class LocationManager {
// in meters

View File

@@ -1,4 +1,7 @@
export declare enum Accuracy {

import promises = require("promises/promises");
export declare enum Accuracy {
// in meters
ANY,
HIGH,
@@ -31,21 +34,31 @@ export declare class Location {
public ios: any; // iOS CLLocation
}
export declare class Options {
export interface Options {
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
desiredAccuracy: number;
desiredAccuracy?: number;
/**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/
updateDistance: number;
updateDistance?: number;
/**
* Minimum time interval between location updates, in milliseconds (android only)
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/
minimumUpdateTime: number;
minimumUpdateTime?: number;
/**
* how old locations to receive in ms.
*/
maximumAge?: number;
/**
* how long to wait for a location in ms.
*/
timeout?: number;
}
export declare class LocationManager {
@@ -98,3 +111,5 @@ export declare class LocationManager {
*/
lastKnownLocation: Location;
}
export declare var getLocation: (options?: Options) => promises.Promise<Location>;

View File

@@ -1,8 +1,11 @@
import types = require("location/location_types");
import common = require("location/location_common");
import merger = require("utils/module_merge");
// merge the exports of the types module with the exports of this file
declare var exports;
require("utils/module_merge").merge(types, exports);
merger.merge(types, exports);
merger.merge(common, exports);
export class LocationManager {

View File

@@ -0,0 +1,70 @@

import types = require("location/location_types");
import promises = require("promises/promises");
import locationModule = require("location/location");
import timer = require("timer/timer");
export var getLocation = function (options?: types.Options) : promises.Promise<types.Location> {
var d = promises.defer<types.Location>();
var timerId;
var locationManager = new locationModule.LocationManager();
if (options && (0 === options.timeout)) {
var location = locationManager.lastKnownLocation;
if (location) {
if (options && ("number" === typeof options.maximumAge)) {
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
d.resolve(location);
}
else {
d.reject(new Error("timeout is 0 and last known location is older than maximumAge"));
}
}
else {
d.resolve(location);
}
}
else {
d.reject(new Error("timeout is 0 and no known location found"));
}
return d.promise();
}
locationManager.startLocationMonitoring(function (location: types.Location) {
if (options && ("number" === typeof options.maximumAge)) {
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.resolve(location);
}
}
else {
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.resolve(location);
}
}, function (error: Error) {
console.error('Location error received: ' + error);
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.reject(error);
},
options
);
if (options && ("number" === typeof options.timeout)) {
timerId = timer.setTimeout(function () {
locationManager.stopLocationMonitoring();
d.reject(new Error("timeout searching for location"));
}, options.timeout);
}
return d.promise();
}

View File

@@ -23,21 +23,31 @@ export class Location {
public ios: any; // iOS native location
}
export class Options {
export interface Options {
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
public desiredAccuracy: number;
desiredAccuracy?: number;
/**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/
public updateDistance: number;
updateDistance?: number;
/**
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/
public minimumUpdateTime: number;
minimumUpdateTime?: number;
/**
* how old locations to receive in ms.
*/
maximumAge?: number;
/**
* how long to wait for a location in ms.
*/
timeout?: number;
}
export class LocationRegion {