@nobody-os/react-native-background-geolocation 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +129 -2
- package/ReactNativeBackgroundGeolocation.podspec +22 -0
- package/android/build.gradle +36 -0
- package/android/src/main/AndroidManifest.xml +9 -0
- package/android/src/main/java/com/pakeku/reactnativebackgroundgeolocation/ReactNativeBackgroundGeolocationModule.kt +185 -0
- package/android/src/main/java/com/pakeku/reactnativebackgroundgeolocation/ReactNativeBackgroundGeolocationPackage.kt +30 -0
- package/ios/ReactNativeBackgroundGeolocation.h +5 -0
- package/ios/ReactNativeBackgroundGeolocation.mm +147 -0
- package/lib/module/NativeReactNativeBackgroundGeolocation.js +15 -0
- package/lib/module/NativeReactNativeBackgroundGeolocation.js.map +1 -0
- package/lib/module/geo/filter.js +37 -0
- package/lib/module/geo/filter.js.map +1 -0
- package/lib/module/geo/recorder.js +278 -0
- package/lib/module/geo/recorder.js.map +1 -0
- package/lib/module/index.js +60 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/location.js +71 -0
- package/lib/module/location.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeReactNativeBackgroundGeolocation.d.ts +66 -0
- package/lib/typescript/src/NativeReactNativeBackgroundGeolocation.d.ts.map +1 -0
- package/lib/typescript/src/geo/filter.d.ts +26 -0
- package/lib/typescript/src/geo/filter.d.ts.map +1 -0
- package/lib/typescript/src/geo/recorder.d.ts +132 -0
- package/lib/typescript/src/geo/recorder.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +44 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/location.d.ts +35 -0
- package/lib/typescript/src/location.d.ts.map +1 -0
- package/package.json +144 -4
- package/src/NativeReactNativeBackgroundGeolocation.ts +73 -0
- package/src/geo/filter.ts +62 -0
- package/src/geo/recorder.ts +395 -0
- package/src/index.tsx +97 -0
- package/src/location.ts +93 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { haversineMeters } from '../location';
|
|
2
|
+
import {
|
|
3
|
+
shouldAcceptFix,
|
|
4
|
+
DEFAULT_FILTER_OPTIONS,
|
|
5
|
+
type FilterOptions,
|
|
6
|
+
type FixLike,
|
|
7
|
+
} from './filter';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* TrackRecorder ingests raw fixes and builds a clean track: total distance,
|
|
11
|
+
* elapsed and moving time, per-kilometer (or per-mile) splits, and stop
|
|
12
|
+
* detection. It is the "Strava brain" of the library, written as pure
|
|
13
|
+
* TypeScript so it can be proven with unit tests and reused by any app
|
|
14
|
+
* built on the native engine.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export type TrackFix = FixLike & {
|
|
18
|
+
altitude: number;
|
|
19
|
+
speed: number;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type Split = {
|
|
23
|
+
/** Split length in meters (1000 unless a custom length was configured). */
|
|
24
|
+
distanceMeters: number;
|
|
25
|
+
/** Duration of this split in milliseconds. */
|
|
26
|
+
durationMs: number;
|
|
27
|
+
/** Average pace for the split, seconds per kilometer. */
|
|
28
|
+
paceSecondsPerKm: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One continuous stretch of a track, bounded by pauses. When the recorder
|
|
33
|
+
* stays stopped for at least `pauseThresholdMs`, the current segment is
|
|
34
|
+
* closed at the last moving fix and the next moving fix opens a new one.
|
|
35
|
+
* The pause itself belongs to no segment.
|
|
36
|
+
*/
|
|
37
|
+
export type TrackSegment = {
|
|
38
|
+
/** Timestamp of the segment's first fix. */
|
|
39
|
+
startTimestamp: number;
|
|
40
|
+
/** Timestamp of the segment's last fix. */
|
|
41
|
+
endTimestamp: number;
|
|
42
|
+
/** endTimestamp - startTimestamp, in milliseconds. */
|
|
43
|
+
durationMs: number;
|
|
44
|
+
/** Distance covered inside the segment, in meters. */
|
|
45
|
+
distanceMeters: number;
|
|
46
|
+
/** Time spent moving inside the segment, in milliseconds. */
|
|
47
|
+
movingMs: number;
|
|
48
|
+
/** Accepted fixes in the segment. */
|
|
49
|
+
fixCount: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Everything an app needs to render a finished (or live) activity. */
|
|
53
|
+
export type TripSummary = {
|
|
54
|
+
distanceMeters: number;
|
|
55
|
+
elapsedMs: number;
|
|
56
|
+
movingMs: number;
|
|
57
|
+
stoppedMs: number;
|
|
58
|
+
/** Sum of positive altitude changes, in meters. Descents are ignored. */
|
|
59
|
+
elevationGainMeters: number;
|
|
60
|
+
/** Moving-time pace, seconds per kilometer. 0 when undefined. */
|
|
61
|
+
averagePaceSecondsPerKm: number;
|
|
62
|
+
fixCount: number;
|
|
63
|
+
segmentCount: number;
|
|
64
|
+
segments: TrackSegment[];
|
|
65
|
+
splits: Split[];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type RecorderOptions = {
|
|
69
|
+
filter?: FilterOptions;
|
|
70
|
+
/** Length of each split in meters. Default 1000 (per-kilometer splits). */
|
|
71
|
+
splitLengthMeters?: number;
|
|
72
|
+
/**
|
|
73
|
+
* A fix counts as "stopped" when its speed (or implied speed from the
|
|
74
|
+
* previous fix) stays below this, in meters per second. Default 0.5.
|
|
75
|
+
*/
|
|
76
|
+
stopSpeedThresholdMps?: number;
|
|
77
|
+
/**
|
|
78
|
+
* A stop lasting at least this long ends the current segment; the next
|
|
79
|
+
* moving fix starts a new one. Default 180000 (3 minutes).
|
|
80
|
+
*/
|
|
81
|
+
pauseThresholdMs?: number;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const DEFAULT_SPLIT_LENGTH = 1000;
|
|
85
|
+
const DEFAULT_STOP_SPEED = 0.5;
|
|
86
|
+
const DEFAULT_PAUSE_THRESHOLD_MS = 180_000;
|
|
87
|
+
|
|
88
|
+
type SegmentAccumulators = {
|
|
89
|
+
fixCount: number;
|
|
90
|
+
distanceMeters: number;
|
|
91
|
+
movingMs: number;
|
|
92
|
+
startTimestamp: number;
|
|
93
|
+
lastTimestamp: number;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export class TrackRecorder {
|
|
97
|
+
private readonly filterOptions: FilterOptions;
|
|
98
|
+
private readonly splitLengthMeters: number;
|
|
99
|
+
private readonly stopSpeedThresholdMps: number;
|
|
100
|
+
private readonly pauseThresholdMs: number;
|
|
101
|
+
|
|
102
|
+
private fixes: TrackFix[] = [];
|
|
103
|
+
private _distanceMeters = 0;
|
|
104
|
+
private _movingMs = 0;
|
|
105
|
+
private _elapsedMs = 0;
|
|
106
|
+
private _elevationGainMeters = 0;
|
|
107
|
+
private _splits: Split[] = [];
|
|
108
|
+
private distanceAtSplitStart = 0;
|
|
109
|
+
private timeAtSplitStart = 0;
|
|
110
|
+
|
|
111
|
+
private completedSegments: TrackSegment[] = [];
|
|
112
|
+
private segment: SegmentAccumulators | null = null;
|
|
113
|
+
/** Stopped time accumulated in the current, not-yet-a-pause stop. */
|
|
114
|
+
private stopEpisodeMs = 0;
|
|
115
|
+
/**
|
|
116
|
+
* Segment state captured when the current stop began. If the stop turns
|
|
117
|
+
* into a pause, the segment is closed from this snapshot, so the stopped
|
|
118
|
+
* fixes never join the segment they interrupted.
|
|
119
|
+
*/
|
|
120
|
+
private stopSnapshot: SegmentAccumulators | null = null;
|
|
121
|
+
|
|
122
|
+
constructor(options: RecorderOptions = {}) {
|
|
123
|
+
this.filterOptions = options.filter ?? DEFAULT_FILTER_OPTIONS;
|
|
124
|
+
this.splitLengthMeters = options.splitLengthMeters ?? DEFAULT_SPLIT_LENGTH;
|
|
125
|
+
this.stopSpeedThresholdMps =
|
|
126
|
+
options.stopSpeedThresholdMps ?? DEFAULT_STOP_SPEED;
|
|
127
|
+
this.pauseThresholdMs =
|
|
128
|
+
options.pauseThresholdMs ?? DEFAULT_PAUSE_THRESHOLD_MS;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Offer a raw fix to the track. Returns true when the fix was accepted
|
|
133
|
+
* and recorded, false when a filter rule dropped it.
|
|
134
|
+
*/
|
|
135
|
+
addFix(fix: TrackFix): boolean {
|
|
136
|
+
const last = this.fixes[this.fixes.length - 1];
|
|
137
|
+
if (!shouldAcceptFix(fix, last, this.filterOptions)) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (last) {
|
|
142
|
+
const segmentMeters = haversineMeters(last, fix);
|
|
143
|
+
const segmentMs = fix.timestamp - last.timestamp;
|
|
144
|
+
this._distanceMeters += segmentMeters;
|
|
145
|
+
this._elapsedMs += segmentMs;
|
|
146
|
+
|
|
147
|
+
const altitudeDelta = fix.altitude - last.altitude;
|
|
148
|
+
if (altitudeDelta > 0) {
|
|
149
|
+
this._elevationGainMeters += altitudeDelta;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const impliedSpeed =
|
|
153
|
+
segmentMs > 0 ? segmentMeters / (segmentMs / 1000) : 0;
|
|
154
|
+
const reportedSpeed = fix.speed >= 0 ? fix.speed : impliedSpeed;
|
|
155
|
+
const pairMoving = reportedSpeed >= this.stopSpeedThresholdMps;
|
|
156
|
+
// Sparse-fix pause: the engine emitted nothing while the user was
|
|
157
|
+
// stopped (stationary fixes are filtered out), so the pause shows up
|
|
158
|
+
// as one long, barely-moving gap before a MOVING fix. (A stopped
|
|
159
|
+
// pair, however long, belongs to the stop-episode accounting below,
|
|
160
|
+
// never to this rule.)
|
|
161
|
+
const isLongPauseGap =
|
|
162
|
+
pairMoving &&
|
|
163
|
+
segmentMs >= this.pauseThresholdMs &&
|
|
164
|
+
impliedSpeed < this.stopSpeedThresholdMps;
|
|
165
|
+
if (pairMoving && !isLongPauseGap) {
|
|
166
|
+
this._movingMs += segmentMs;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.recordSegmentPair(
|
|
170
|
+
fix,
|
|
171
|
+
segmentMeters,
|
|
172
|
+
segmentMs,
|
|
173
|
+
pairMoving,
|
|
174
|
+
isLongPauseGap
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
this._distanceMeters - this.distanceAtSplitStart >=
|
|
179
|
+
this.splitLengthMeters
|
|
180
|
+
) {
|
|
181
|
+
const splitDuration = fix.timestamp - this.timeAtSplitStart;
|
|
182
|
+
const splitDistance = this._distanceMeters - this.distanceAtSplitStart;
|
|
183
|
+
this._splits.push({
|
|
184
|
+
distanceMeters: splitDistance,
|
|
185
|
+
durationMs: splitDuration,
|
|
186
|
+
paceSecondsPerKm:
|
|
187
|
+
splitDistance > 0
|
|
188
|
+
? splitDuration / 1000 / (splitDistance / 1000)
|
|
189
|
+
: 0,
|
|
190
|
+
});
|
|
191
|
+
this.distanceAtSplitStart = this._distanceMeters;
|
|
192
|
+
this.timeAtSplitStart = fix.timestamp;
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
this.timeAtSplitStart = fix.timestamp;
|
|
196
|
+
this.segment = {
|
|
197
|
+
fixCount: 1,
|
|
198
|
+
distanceMeters: 0,
|
|
199
|
+
movingMs: 0,
|
|
200
|
+
startTimestamp: fix.timestamp,
|
|
201
|
+
lastTimestamp: fix.timestamp,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.fixes.push(fix);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Fold one accepted pair (previous fix -> this fix) into the segment
|
|
211
|
+
* bookkeeping, closing and reopening segments around pauses.
|
|
212
|
+
*/
|
|
213
|
+
private recordSegmentPair(
|
|
214
|
+
fix: TrackFix,
|
|
215
|
+
segmentMeters: number,
|
|
216
|
+
segmentMs: number,
|
|
217
|
+
pairMoving: boolean,
|
|
218
|
+
isLongPauseGap: boolean
|
|
219
|
+
): void {
|
|
220
|
+
if (!this.segment) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (isLongPauseGap) {
|
|
225
|
+
// Close the segment at the last fix before the gap (the snapshot if
|
|
226
|
+
// a stop episode was in progress, else the live accumulators) and
|
|
227
|
+
// open a new segment at this fix.
|
|
228
|
+
const closing = this.stopSnapshot ?? this.segment;
|
|
229
|
+
this.completedSegments.push(toTrackSegment(closing));
|
|
230
|
+
this.segment = {
|
|
231
|
+
fixCount: 1,
|
|
232
|
+
distanceMeters: 0,
|
|
233
|
+
movingMs: 0,
|
|
234
|
+
startTimestamp: fix.timestamp,
|
|
235
|
+
lastTimestamp: fix.timestamp,
|
|
236
|
+
};
|
|
237
|
+
this.stopEpisodeMs = 0;
|
|
238
|
+
this.stopSnapshot = null;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (pairMoving) {
|
|
243
|
+
if (
|
|
244
|
+
this.stopSnapshot !== null &&
|
|
245
|
+
this.stopEpisodeMs >= this.pauseThresholdMs
|
|
246
|
+
) {
|
|
247
|
+
// The stop became a pause: close the segment where movement last
|
|
248
|
+
// happened, and let this fix open a fresh segment. The bridging
|
|
249
|
+
// distance (GPS drift at a standstill) counts toward the track
|
|
250
|
+
// totals but belongs to neither segment.
|
|
251
|
+
this.completedSegments.push(toTrackSegment(this.stopSnapshot));
|
|
252
|
+
this.segment = {
|
|
253
|
+
fixCount: 1,
|
|
254
|
+
distanceMeters: 0,
|
|
255
|
+
movingMs: 0,
|
|
256
|
+
startTimestamp: fix.timestamp,
|
|
257
|
+
lastTimestamp: fix.timestamp,
|
|
258
|
+
};
|
|
259
|
+
} else {
|
|
260
|
+
this.segment.fixCount += 1;
|
|
261
|
+
this.segment.distanceMeters += segmentMeters;
|
|
262
|
+
this.segment.movingMs += segmentMs;
|
|
263
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
264
|
+
}
|
|
265
|
+
this.stopEpisodeMs = 0;
|
|
266
|
+
this.stopSnapshot = null;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Stopped pair: tentatively part of the current segment, unless the
|
|
271
|
+
// stop grows into a pause, in which case the snapshot taken here is
|
|
272
|
+
// what gets closed.
|
|
273
|
+
if (this.stopSnapshot === null) {
|
|
274
|
+
this.stopSnapshot = { ...this.segment };
|
|
275
|
+
}
|
|
276
|
+
this.stopEpisodeMs += segmentMs;
|
|
277
|
+
this.segment.fixCount += 1;
|
|
278
|
+
this.segment.distanceMeters += segmentMeters;
|
|
279
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Accepted fixes, in order. */
|
|
283
|
+
get points(): TrackFix[] {
|
|
284
|
+
return [...this.fixes];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
get distanceMeters(): number {
|
|
288
|
+
return this._distanceMeters;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Wall-clock time from first to last accepted fix, in milliseconds. */
|
|
292
|
+
get elapsedMs(): number {
|
|
293
|
+
return this._elapsedMs;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Time actually spent moving (stops excluded), in milliseconds. */
|
|
297
|
+
get movingMs(): number {
|
|
298
|
+
return this._movingMs;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Elapsed minus moving: time spent standing still, in milliseconds. */
|
|
302
|
+
get stoppedMs(): number {
|
|
303
|
+
return this._elapsedMs - this._movingMs;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** True when the most recent movement looks like a stop. */
|
|
307
|
+
get isStopped(): boolean {
|
|
308
|
+
if (this.fixes.length < 2) {
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
const last = this.fixes[this.fixes.length - 1];
|
|
312
|
+
return last !== undefined && last.speed >= 0
|
|
313
|
+
? last.speed < this.stopSpeedThresholdMps
|
|
314
|
+
: false;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Completed splits so far. */
|
|
318
|
+
get splits(): Split[] {
|
|
319
|
+
return [...this._splits];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Total climbing, in meters: positive altitude changes only. */
|
|
323
|
+
get elevationGainMeters(): number {
|
|
324
|
+
return this._elevationGainMeters;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* All segments, finished ones plus the one still in progress. When the
|
|
329
|
+
* recorder is sitting in a pause that has passed the threshold, the
|
|
330
|
+
* interrupted segment is already shown closed at its last moving fix.
|
|
331
|
+
*/
|
|
332
|
+
get segments(): TrackSegment[] {
|
|
333
|
+
const visible = [...this.completedSegments];
|
|
334
|
+
if (
|
|
335
|
+
this.stopSnapshot !== null &&
|
|
336
|
+
this.stopEpisodeMs >= this.pauseThresholdMs
|
|
337
|
+
) {
|
|
338
|
+
visible.push(toTrackSegment(this.stopSnapshot));
|
|
339
|
+
} else if (this.segment !== null && this.segment.fixCount > 0) {
|
|
340
|
+
visible.push(toTrackSegment(this.segment));
|
|
341
|
+
}
|
|
342
|
+
return visible;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** One-call snapshot of the whole track, for rendering an activity. */
|
|
346
|
+
summary(): TripSummary {
|
|
347
|
+
const segments = this.segments;
|
|
348
|
+
return {
|
|
349
|
+
distanceMeters: this._distanceMeters,
|
|
350
|
+
elapsedMs: this._elapsedMs,
|
|
351
|
+
movingMs: this._movingMs,
|
|
352
|
+
stoppedMs: this.stoppedMs,
|
|
353
|
+
elevationGainMeters: this._elevationGainMeters,
|
|
354
|
+
averagePaceSecondsPerKm: this.averagePaceSecondsPerKm,
|
|
355
|
+
fixCount: this.fixes.length,
|
|
356
|
+
segmentCount: segments.length,
|
|
357
|
+
segments,
|
|
358
|
+
splits: this.splits,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Average pace over the whole track, seconds per kilometer. */
|
|
363
|
+
get averagePaceSecondsPerKm(): number {
|
|
364
|
+
if (this._distanceMeters === 0 || this._movingMs === 0) {
|
|
365
|
+
return 0;
|
|
366
|
+
}
|
|
367
|
+
return this._movingMs / 1000 / (this._distanceMeters / 1000);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
reset(): void {
|
|
371
|
+
this.fixes = [];
|
|
372
|
+
this._distanceMeters = 0;
|
|
373
|
+
this._movingMs = 0;
|
|
374
|
+
this._elapsedMs = 0;
|
|
375
|
+
this._elevationGainMeters = 0;
|
|
376
|
+
this._splits = [];
|
|
377
|
+
this.distanceAtSplitStart = 0;
|
|
378
|
+
this.timeAtSplitStart = 0;
|
|
379
|
+
this.completedSegments = [];
|
|
380
|
+
this.segment = null;
|
|
381
|
+
this.stopEpisodeMs = 0;
|
|
382
|
+
this.stopSnapshot = null;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function toTrackSegment(acc: SegmentAccumulators): TrackSegment {
|
|
387
|
+
return {
|
|
388
|
+
startTimestamp: acc.startTimestamp,
|
|
389
|
+
endTimestamp: acc.lastTimestamp,
|
|
390
|
+
durationMs: acc.lastTimestamp - acc.startTimestamp,
|
|
391
|
+
distanceMeters: acc.distanceMeters,
|
|
392
|
+
movingMs: acc.movingMs,
|
|
393
|
+
fixCount: acc.fixCount,
|
|
394
|
+
};
|
|
395
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import NativeBackgroundGeolocation, {
|
|
2
|
+
type GeolocationConfig,
|
|
3
|
+
type GeolocationError,
|
|
4
|
+
type LocationFix,
|
|
5
|
+
} from './NativeReactNativeBackgroundGeolocation';
|
|
6
|
+
|
|
7
|
+
export type { GeolocationConfig, GeolocationError, LocationFix };
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
haversineMeters,
|
|
11
|
+
totalDistanceMeters,
|
|
12
|
+
filterByAccuracy,
|
|
13
|
+
durationSeconds,
|
|
14
|
+
averagePaceMinPerKm,
|
|
15
|
+
type Coordinate,
|
|
16
|
+
type GeoFix,
|
|
17
|
+
} from './location';
|
|
18
|
+
export {
|
|
19
|
+
shouldAcceptFix,
|
|
20
|
+
hasAcceptableAccuracy,
|
|
21
|
+
isChronological,
|
|
22
|
+
isFarEnough,
|
|
23
|
+
DEFAULT_FILTER_OPTIONS,
|
|
24
|
+
type FilterOptions,
|
|
25
|
+
type FixLike,
|
|
26
|
+
} from './geo/filter';
|
|
27
|
+
export {
|
|
28
|
+
TrackRecorder,
|
|
29
|
+
type TrackFix,
|
|
30
|
+
type Split,
|
|
31
|
+
type TrackSegment,
|
|
32
|
+
type TripSummary,
|
|
33
|
+
type RecorderOptions,
|
|
34
|
+
} from './geo/recorder';
|
|
35
|
+
|
|
36
|
+
type ListenerSubscription = ReturnType<
|
|
37
|
+
typeof NativeBackgroundGeolocation.onLocation
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Start background location tracking.
|
|
42
|
+
*
|
|
43
|
+
* The app is responsible for holding location permission (and, for true
|
|
44
|
+
* background tracking, the background location permission) before calling
|
|
45
|
+
* this. The promise rejects with code E_PERMISSION when it does not.
|
|
46
|
+
*/
|
|
47
|
+
export function start(config: GeolocationConfig = {}): Promise<void> {
|
|
48
|
+
return NativeBackgroundGeolocation.start(config);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Stop tracking and release the native engine. */
|
|
52
|
+
export function stop(): Promise<void> {
|
|
53
|
+
return NativeBackgroundGeolocation.stop();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One-shot position query: ask the OS where the device is right now,
|
|
58
|
+
* without starting continuous tracking.
|
|
59
|
+
*/
|
|
60
|
+
export function getCurrentPosition(): Promise<LocationFix> {
|
|
61
|
+
return NativeBackgroundGeolocation.getCurrentPosition();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Whether native tracking is currently running. */
|
|
65
|
+
export function isTracking(): Promise<boolean> {
|
|
66
|
+
return NativeBackgroundGeolocation.isTracking();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Subscribe to fixes while tracking. Returns a subscription to remove.
|
|
71
|
+
*
|
|
72
|
+
* With codegen EventEmitter properties, the spec property itself is the
|
|
73
|
+
* subscribe function. Do NOT route this through NativeEventEmitter: its
|
|
74
|
+
* legacy addListener call throws inside the new-architecture TurboModule
|
|
75
|
+
* invocation layer and aborts the app (found the hard way, build 7).
|
|
76
|
+
*/
|
|
77
|
+
export function addLocationListener(
|
|
78
|
+
listener: (fix: LocationFix) => void
|
|
79
|
+
): ListenerSubscription {
|
|
80
|
+
return NativeBackgroundGeolocation.onLocation(listener);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Subscribe to native engine errors. Returns a subscription to remove. */
|
|
84
|
+
export function addErrorListener(
|
|
85
|
+
listener: (error: GeolocationError) => void
|
|
86
|
+
): ListenerSubscription {
|
|
87
|
+
return NativeBackgroundGeolocation.onError(listener);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default {
|
|
91
|
+
start,
|
|
92
|
+
stop,
|
|
93
|
+
getCurrentPosition,
|
|
94
|
+
isTracking,
|
|
95
|
+
addLocationListener,
|
|
96
|
+
addErrorListener,
|
|
97
|
+
};
|
package/src/location.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Pure location math for @nobody-os/react-native-background-geolocation.
|
|
2
|
+
//
|
|
3
|
+
// No React Native imports here on purpose: these are pure functions, so they
|
|
4
|
+
// are testable with Jest on any machine (including Windows, no phone, no
|
|
5
|
+
// emulator) and they are the engine the native bridge feeds into.
|
|
6
|
+
// Earth radius 6,371,000 m (mean radius), haversine formula.
|
|
7
|
+
|
|
8
|
+
export interface GeoFix {
|
|
9
|
+
latitude: number;
|
|
10
|
+
longitude: number;
|
|
11
|
+
/** Unix epoch milliseconds, from the OS location timestamp. */
|
|
12
|
+
timestampMs: number;
|
|
13
|
+
/** Metres above sea level, when the OS reports it. */
|
|
14
|
+
altitude?: number;
|
|
15
|
+
/** Horizontal accuracy in metres, when the OS reports it. Lower is better. */
|
|
16
|
+
accuracy?: number;
|
|
17
|
+
/** Speed in m/s, when the OS reports it. */
|
|
18
|
+
speed?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const EARTH_RADIUS_M = 6_371_000;
|
|
22
|
+
|
|
23
|
+
function toRadians(degrees: number): number {
|
|
24
|
+
return (degrees * Math.PI) / 180;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Anything with a latitude and a longitude: a fix, a waypoint, a pin. */
|
|
28
|
+
export type Coordinate = {
|
|
29
|
+
latitude: number;
|
|
30
|
+
longitude: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** Great-circle distance between two coordinates, in metres. */
|
|
34
|
+
export function haversineMeters(a: Coordinate, b: Coordinate): number {
|
|
35
|
+
const dLat = toRadians(b.latitude - a.latitude);
|
|
36
|
+
const dLon = toRadians(b.longitude - a.longitude);
|
|
37
|
+
const lat1 = toRadians(a.latitude);
|
|
38
|
+
const lat2 = toRadians(b.latitude);
|
|
39
|
+
const h =
|
|
40
|
+
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
41
|
+
Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
42
|
+
return 2 * EARTH_RADIUS_M * Math.asin(Math.sqrt(h));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Sum of haversine distances between consecutive points, in metres. */
|
|
46
|
+
export function totalDistanceMeters(fixes: readonly Coordinate[]): number {
|
|
47
|
+
let total = 0;
|
|
48
|
+
for (let i = 1; i < fixes.length; i += 1) {
|
|
49
|
+
const prev = fixes[i - 1];
|
|
50
|
+
const curr = fixes[i];
|
|
51
|
+
if (prev !== undefined && curr !== undefined) {
|
|
52
|
+
total += haversineMeters(prev, curr);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return total;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Drop fixes whose reported accuracy is worse than `maxAccuracyM`.
|
|
60
|
+
* Fixes with no accuracy reading are kept: the OS gave us no reason to
|
|
61
|
+
* distrust them. Default 50 m matches a typical running-track tolerance.
|
|
62
|
+
*/
|
|
63
|
+
export function filterByAccuracy(
|
|
64
|
+
fixes: readonly GeoFix[],
|
|
65
|
+
maxAccuracyM = 50
|
|
66
|
+
): GeoFix[] {
|
|
67
|
+
return fixes.filter(
|
|
68
|
+
(fix) => fix.accuracy === undefined || fix.accuracy <= maxAccuracyM
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Elapsed time between the first and last fix, in seconds. */
|
|
73
|
+
export function durationSeconds(fixes: readonly GeoFix[]): number {
|
|
74
|
+
const first = fixes[0];
|
|
75
|
+
const last = fixes[fixes.length - 1];
|
|
76
|
+
if (first === undefined || last === undefined) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
return Math.max(0, (last.timestampMs - first.timestampMs) / 1000);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Average pace in minutes per kilometre. Returns null when there is no
|
|
84
|
+
* usable distance or duration (avoids a divide-by-zero pace of Infinity).
|
|
85
|
+
*/
|
|
86
|
+
export function averagePaceMinPerKm(fixes: readonly GeoFix[]): number | null {
|
|
87
|
+
const distanceKm = totalDistanceMeters(fixes) / 1000;
|
|
88
|
+
const durationMin = durationSeconds(fixes) / 60;
|
|
89
|
+
if (distanceKm <= 0 || durationMin <= 0) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
return durationMin / distanceKm;
|
|
93
|
+
}
|