forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation-common.ts
More file actions
96 lines (83 loc) · 3.41 KB
/
location-common.ts
File metadata and controls
96 lines (83 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import timer = require("timer");
//import locationManagerModule = require("location/location-manager");
import defModule = require("location");
// merge the exports of the types module with the exports of this file
//import merger = require("utils/module-merge");
//declare var exports;
//merger.merge(locationManagerModule, exports);
var defaultGetLocationTimeout = 20000;
export class Location implements defModule.Location {
public latitude: number;
public longitude: number;
public altitude: number;
public horizontalAccuracy: number;
public verticalAccuracy: number;
public speed: number; // in m/s ?
public direction: number; // in degrees
public timestamp: Date;
public android: any; // android Location
public ios: any; // iOS native location
}
export var getLocation = function (options?: defModule.Options): Promise<defModule.Location> {
var timerId;
var locationManager = new defModule.LocationManager();
if (options && (0 === options.timeout)) {
return new Promise<defModule.Location>((resolve, reject) => {
var location = locationManager.lastKnownLocation;
if (location) {
if (options && ("number" === typeof options.maximumAge)) {
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
resolve(location);
}
else {
reject(new Error("timeout is 0 and last known location is older than maximumAge"));
}
}
else {
resolve(location);
}
}
else {
reject(new Error("timeout is 0 and no known location found"));
}
});
}
return new Promise<defModule.Location>((resolve, reject) => {
if (!defModule.LocationManager.isEnabled()) {
return reject(new Error("Location service is disabled"));
}
locationManager.startLocationMonitoring(function (location: defModule.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);
}
resolve(location);
}
}
else {
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
resolve(location);
}
}, function (error: Error) {
console.error('Location error received: ' + error);
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
reject(error);
},
options
);
if (options && ("number" === typeof options.timeout)) {
timerId = timer.setTimeout(function () {
locationManager.stopLocationMonitoring();
reject(new Error("timeout searching for location"));
}, options.timeout || defaultGetLocationTimeout);
}
});
}