This repository was archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtrack.js
194 lines (143 loc) · 4 KB
/
track.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Library to do the actual tracking.
*/
// DEPENDENCIES
var dialogs = require('alloy/dialogs');
var permissions = require('permissions');
var utils = require('utils');
// PUBLIC INTERFACE
var $ = module.exports = _.extend({
getCurrentRide: getCurrentRide,
isTracking: isTracking,
toggleTracking: toggleTracking,
startTracking: startTracking,
stopTracking: stopTracking
}, Backbone.Events);
// PRIVATE VARIABLES
var currentRide;
var configuredMonitoring = false;
// PRIVATE FUNCTIONS
function getCurrentRide() {
return currentRide;
}
function isTracking() {
return !!getCurrentRide();
}
function toggleTracking(cb) {
if (isTracking()) {
stopTracking(cb);
} else {
startTracking(cb);
}
}
function startTracking(cb) {
if (isTracking()) {
return cb({
success: false,
error: 'Already tracking'
});
}
initMonitoring(function(e) {
if (!e.success) {
return cb(e);
}
currentRide = Alloy.Collections.instance('ride').create({
id: utils.guid(),
fromTime: Date.now()
});
Ti.Geolocation.addEventListener('location', onLocation);
if (OS_IOS) {
Ti.Geolocation.addEventListener('locationupdatepaused', onLocationupdate);
Ti.Geolocation.addEventListener('locationupdateresumed', onLocationupdate);
}
cb({
success: true
});
$.trigger('state state:start', {
type: 'start'
});
});
}
function stopTracking(cb) {
if (!isTracking()) {
return cb({
success: false,
error: 'Not tracking'
});
}
Ti.Geolocation.removeEventListener('location', onLocation);
if (OS_IOS) {
Ti.Geolocation.removeEventListener('locationupdatepaused', onLocationupdate);
Ti.Geolocation.removeEventListener('locationupdateresumed', onLocationupdate);
}
currentRide.save({
toTime: Date.now()
});
currentRide = null;
cb({
success: true
});
$.trigger('state state:stop', {
type: 'stop'
});
}
function initMonitoring(cb) {
permissions.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
if (e.success && !configuredMonitoring) {
if (OS_IOS) {
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = Alloy.CFG.minUpdateDistance;
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.pauseLocationUpdateAutomatically = true;
Ti.Geolocation.activityType = Ti.Geolocation.ACTIVITYTYPE_FITNESS;
}
if (OS_ANDROID) {
Ti.Geolocation.Android.addLocationProvider(Ti.Geolocation.Android.createLocationProvider({
name: Ti.Geolocation.PROVIDER_GPS,
minUpdateDistance: Alloy.CFG.minUpdateDistance,
minUpdateTime: (Alloy.CFG.minAge / 1000)
}));
Ti.Geolocation.Android.addLocationRule(Ti.Geolocation.Android.createLocationRule({
provider: Ti.Geolocation.PROVIDER_GPS,
accuracy: Alloy.CFG.accuracy,
maxAge: Alloy.CFG.maxAge,
minAge: Alloy.CFG.minAge
}));
Ti.Geolocation.Android.manualMode = true;
}
configuredMonitoring = true;
}
return cb(e);
});
}
function onLocation(e) {
if (!e.error) {
var coords = e.coords;
var data = {
id: utils.guid(),
ride: currentRide.id
};
// iOS: -1, Android: null
if (coords.altitudeAccuracy !== -1 && coords.altitudeAccuracy !== null) {
data.altitudeAccuracy = coords.altitudeAccuracy;
data.altitude = coords.altitude;
}
['heading', 'speed'].forEach(function(key) {
if (coords[key] !== -1) {
data[key] = coords[key];
}
});
['altitude', 'latitude', 'longitude', 'timestamp'].forEach(function(key) {
data[key] = coords[key];
});
Alloy.Collections.instance('data').create(data);
data.type = 'location';
$.trigger('data', data);
}
}
function onLocationupdate(e) {
var state = (e.type === 'locationupdatepaused' ? 'pause' : 'resume');
$.trigger('state state:' + state, {
type: state
});
}