-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlocation.js
71 lines (56 loc) · 1.58 KB
/
location.js
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
var callback, error;
var timeout;
var location = {};
function firstLocation(e) {
if (e.success === undefined || e.success) {
if ( typeof (callback) === 'function') {
clearTimeout(timeout);
setLocation(e);
log();
callback(location);
Ti.Geolocation.removeEventListener("location", firstLocation);
// Might get more accurate readings if we leave a listener open.
Ti.Geolocation.addEventListener("location", subsequentLocations);
}
}
}
function subsequentLocations(e) {
if ( typeof (callback) === 'function') {
setLocation(e);
log();
}
}
function setLocation(e) {
if(e.coords) {
location.latitude = e.coords.latitude;
location.longitude = e.coords.longitude;
}
}
function log() {
Ti.API.info('latitude is: ' + location.latitude);
Ti.API.info('longitude is: ' + location.longitude);
}
function start(obj) {
callback = obj.action;
error = obj.error || function(){};
timeout = setTimeout(function() {
error();
stop();
}, 25000);
Titanium.Geolocation.distanceFilter = 1;
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.frequency = 1;
Ti.Geolocation.addEventListener("location", firstLocation);
}
function stop() {
callback = undefined;
error = undefined;
clearTimeout(timeout);
timeout = undefined;
location = {};
Ti.Geolocation.removeEventListener("location", firstLocation);
Ti.Geolocation.removeEventListener("location", subsequentLocations);
}
exports.start = start;
exports.stop = stop;