@nobody-os/react-native-background-geolocation 0.0.0-stage → 0.1.1
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 +127 -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 +121 -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 +65 -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 +170 -0
- package/src/index.tsx +95 -0
- package/src/location.ts +93 -0
package/package.json
CHANGED
|
@@ -1,6 +1,146 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nobody-os/react-native-background-geolocation",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Open-source background geolocation for React Native: continuous background location tracking for iOS and Android, MIT licensed.",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"pakeku-react-native-background-geolocation-source": "./src/index.tsx",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"cpp",
|
|
21
|
+
"*.podspec",
|
|
22
|
+
"react-native.config.js",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/build",
|
|
25
|
+
"!android/gradle",
|
|
26
|
+
"!android/gradlew",
|
|
27
|
+
"!android/gradlew.bat",
|
|
28
|
+
"!android/local.properties",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/__fixtures__",
|
|
31
|
+
"!**/__mocks__",
|
|
32
|
+
"!**/.*"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"example": "yarn workspace @nobody-os/react-native-background-geolocation-example",
|
|
36
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
|
|
37
|
+
"prepare": "bob build",
|
|
38
|
+
"typecheck": "tsc",
|
|
39
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
40
|
+
"test": "jest"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"react-native",
|
|
44
|
+
"ios",
|
|
45
|
+
"android"
|
|
46
|
+
],
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/pakeku/react-native-background-geolocation.git"
|
|
50
|
+
},
|
|
51
|
+
"author": "Erick Pacheco <e@pacheco.sh> (https://github.com/pakeku)",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/pakeku/react-native-background-geolocation/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://github.com/pakeku/react-native-background-geolocation#readme",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@eslint/compat": "^2.1.1",
|
|
62
|
+
"@eslint/eslintrc": "^3.3.7",
|
|
63
|
+
"@eslint/js": "^9.39.5",
|
|
64
|
+
"@react-native/babel-preset": "0.88.0",
|
|
65
|
+
"@react-native/eslint-config": "0.88.0",
|
|
66
|
+
"@types/jest": "^29.5.14",
|
|
67
|
+
"@types/react": "^19.2.0",
|
|
68
|
+
"babel-jest": "^29.7.0",
|
|
69
|
+
"del-cli": "^8.0.1",
|
|
70
|
+
"eslint": "^9.39.5",
|
|
71
|
+
"eslint-config-prettier": "^10.1.8",
|
|
72
|
+
"eslint-plugin-ft-flow": "^3.0.11",
|
|
73
|
+
"eslint-plugin-prettier": "^5.5.7",
|
|
74
|
+
"jest": "^29.7.0",
|
|
75
|
+
"prettier": "^3.9.10",
|
|
76
|
+
"react": "19.3.0",
|
|
77
|
+
"react-native": "0.88.0",
|
|
78
|
+
"react-native-builder-bob": "^0.44.1",
|
|
79
|
+
"turbo": "^2.11.10",
|
|
80
|
+
"typescript": "^6.0.3"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"react": "*",
|
|
84
|
+
"react-native": "*"
|
|
85
|
+
},
|
|
86
|
+
"workspaces": [
|
|
87
|
+
"example"
|
|
88
|
+
],
|
|
89
|
+
"packageManager": "yarn@4.18.1",
|
|
90
|
+
"react-native-builder-bob": {
|
|
91
|
+
"source": "src",
|
|
92
|
+
"output": "lib",
|
|
93
|
+
"targets": [
|
|
94
|
+
[
|
|
95
|
+
"module",
|
|
96
|
+
{
|
|
97
|
+
"esm": true
|
|
98
|
+
}
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
"typescript",
|
|
102
|
+
{
|
|
103
|
+
"project": "tsconfig.build.json"
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
]
|
|
107
|
+
},
|
|
108
|
+
"codegenConfig": {
|
|
109
|
+
"name": "ReactNativeBackgroundGeolocationSpec",
|
|
110
|
+
"type": "modules",
|
|
111
|
+
"jsSrcsDir": "src",
|
|
112
|
+
"android": {
|
|
113
|
+
"javaPackageName": "com.pakeku.reactnativebackgroundgeolocation"
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"prettier": {
|
|
117
|
+
"quoteProps": "consistent",
|
|
118
|
+
"singleQuote": true,
|
|
119
|
+
"tabWidth": 2,
|
|
120
|
+
"trailingComma": "es5",
|
|
121
|
+
"useTabs": false
|
|
122
|
+
},
|
|
123
|
+
"create-react-native-library": {
|
|
124
|
+
"type": "turbo-module",
|
|
125
|
+
"languages": "kotlin-objc",
|
|
126
|
+
"tools": [
|
|
127
|
+
"eslint"
|
|
128
|
+
],
|
|
129
|
+
"version": "0.64.0"
|
|
130
|
+
},
|
|
131
|
+
"jest": {
|
|
132
|
+
"transform": {
|
|
133
|
+
"^.+.(ts|tsx)$": [
|
|
134
|
+
"babel-jest",
|
|
135
|
+
{
|
|
136
|
+
"presets": [
|
|
137
|
+
"@react-native/babel-preset"
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
},
|
|
142
|
+
"testMatch": [
|
|
143
|
+
"**/src/__tests__/**/*.test.ts?(x)"
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { TurboModuleRegistry, type TurboModule } from 'react-native';
|
|
2
|
+
import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Codegen spec for the ReactNativeBackgroundGeolocation TurboModule.
|
|
6
|
+
*
|
|
7
|
+
* This file is the contract between TypeScript and the native engines.
|
|
8
|
+
* React Native's codegen generates the Kotlin and Objective-C++ bindings
|
|
9
|
+
* from the types declared here, so every public type must stay
|
|
10
|
+
* codegen-compatible (plain object type aliases, no interfaces for data).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type GeolocationConfig = {
|
|
14
|
+
/**
|
|
15
|
+
* Accuracy profile: 'high' (GPS, for run/walk tracking),
|
|
16
|
+
* 'balanced' (default), or 'low' (network/cell, battery-sipping).
|
|
17
|
+
*/
|
|
18
|
+
desiredAccuracy?: string;
|
|
19
|
+
/** Minimum distance in meters between fixes. Default 10. */
|
|
20
|
+
distanceFilter?: number;
|
|
21
|
+
/** Android: desired interval in milliseconds between fixes. Default 5000. */
|
|
22
|
+
interval?: number;
|
|
23
|
+
/** Android: fastest interval in milliseconds the app will accept. Default 2000. */
|
|
24
|
+
fastestInterval?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Minutes of detected stillness after which tracking pauses itself
|
|
27
|
+
* (stop detection). Set to 0 to disable. Default 5.
|
|
28
|
+
*/
|
|
29
|
+
stopTimeout?: number;
|
|
30
|
+
/** Verbose native logging while developing. Default false. */
|
|
31
|
+
debug?: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type LocationFix = {
|
|
35
|
+
latitude: number;
|
|
36
|
+
longitude: number;
|
|
37
|
+
/** Horizontal accuracy in meters (radius of uncertainty). */
|
|
38
|
+
accuracy: number;
|
|
39
|
+
/** Altitude in meters above sea level, or -1 when unavailable. */
|
|
40
|
+
altitude: number;
|
|
41
|
+
/** Speed in meters per second, or -1 when unavailable. */
|
|
42
|
+
speed: number;
|
|
43
|
+
/** Heading in degrees clockwise from north, or -1 when unavailable. */
|
|
44
|
+
heading: number;
|
|
45
|
+
/** Milliseconds since the Unix epoch, as reported by the OS. */
|
|
46
|
+
timestamp: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type GeolocationError = {
|
|
50
|
+
/** 1 = permission denied, 2 = position unavailable, 3 = timeout. */
|
|
51
|
+
code: number;
|
|
52
|
+
message: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export interface Spec extends TurboModule {
|
|
56
|
+
/** Start background tracking with the given configuration. */
|
|
57
|
+
start(config: GeolocationConfig): Promise<void>;
|
|
58
|
+
/** Stop tracking and release native resources. */
|
|
59
|
+
stop(): Promise<void>;
|
|
60
|
+
/** One-shot position query: ask the OS where the device is right now. */
|
|
61
|
+
getCurrentPosition(): Promise<LocationFix>;
|
|
62
|
+
/** Whether native tracking is currently running. */
|
|
63
|
+
isTracking(): Promise<boolean>;
|
|
64
|
+
|
|
65
|
+
/** Fires for every accepted fix while tracking. */
|
|
66
|
+
readonly onLocation: EventEmitter<LocationFix>;
|
|
67
|
+
/** Fires when the native engine reports an error. */
|
|
68
|
+
readonly onError: EventEmitter<GeolocationError>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export default TurboModuleRegistry.getEnforcing<Spec>(
|
|
72
|
+
'ReactNativeBackgroundGeolocation'
|
|
73
|
+
);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { haversineMeters, type Coordinate } from '../location';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fix acceptance rules. Raw GPS is noisy: fixes arrive with poor accuracy,
|
|
5
|
+
* out of order, or jittering around a point while the device stands still.
|
|
6
|
+
* A fix is only worth recording when it passes every rule below.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type FilterOptions = {
|
|
10
|
+
/** Fixes with accuracy worse (larger) than this, in meters, are dropped. */
|
|
11
|
+
maxAccuracyMeters: number;
|
|
12
|
+
/** Fixes closer than this to the last accepted fix, in meters, are dropped. */
|
|
13
|
+
minDistanceMeters: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_FILTER_OPTIONS: FilterOptions = {
|
|
17
|
+
maxAccuracyMeters: 50,
|
|
18
|
+
minDistanceMeters: 5,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type FixLike = Coordinate & {
|
|
22
|
+
accuracy: number;
|
|
23
|
+
timestamp: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** True when the fix's own reported accuracy is good enough to trust. */
|
|
27
|
+
export function hasAcceptableAccuracy(
|
|
28
|
+
fix: FixLike,
|
|
29
|
+
options: FilterOptions
|
|
30
|
+
): boolean {
|
|
31
|
+
return fix.accuracy >= 0 && fix.accuracy <= options.maxAccuracyMeters;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** True when the fix is newer than the last accepted one (no time travel). */
|
|
35
|
+
export function isChronological(fix: FixLike, last?: FixLike): boolean {
|
|
36
|
+
return !last || fix.timestamp > last.timestamp;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** True when the fix moved far enough from the last accepted fix to matter. */
|
|
40
|
+
export function isFarEnough(
|
|
41
|
+
fix: FixLike,
|
|
42
|
+
last: FixLike | undefined,
|
|
43
|
+
options: FilterOptions
|
|
44
|
+
): boolean {
|
|
45
|
+
if (!last) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return haversineMeters(last, fix) >= options.minDistanceMeters;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** All rules combined: should this fix be recorded? */
|
|
52
|
+
export function shouldAcceptFix(
|
|
53
|
+
fix: FixLike,
|
|
54
|
+
last: FixLike | undefined,
|
|
55
|
+
options: FilterOptions = DEFAULT_FILTER_OPTIONS
|
|
56
|
+
): boolean {
|
|
57
|
+
return (
|
|
58
|
+
hasAcceptableAccuracy(fix, options) &&
|
|
59
|
+
isChronological(fix, last) &&
|
|
60
|
+
isFarEnough(fix, last, options)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
export type RecorderOptions = {
|
|
32
|
+
filter?: FilterOptions;
|
|
33
|
+
/** Length of each split in meters. Default 1000 (per-kilometer splits). */
|
|
34
|
+
splitLengthMeters?: number;
|
|
35
|
+
/**
|
|
36
|
+
* A fix counts as "stopped" when its speed (or implied speed from the
|
|
37
|
+
* previous fix) stays below this, in meters per second. Default 0.5.
|
|
38
|
+
*/
|
|
39
|
+
stopSpeedThresholdMps?: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const DEFAULT_SPLIT_LENGTH = 1000;
|
|
43
|
+
const DEFAULT_STOP_SPEED = 0.5;
|
|
44
|
+
|
|
45
|
+
export class TrackRecorder {
|
|
46
|
+
private readonly filterOptions: FilterOptions;
|
|
47
|
+
private readonly splitLengthMeters: number;
|
|
48
|
+
private readonly stopSpeedThresholdMps: number;
|
|
49
|
+
|
|
50
|
+
private fixes: TrackFix[] = [];
|
|
51
|
+
private _distanceMeters = 0;
|
|
52
|
+
private _movingMs = 0;
|
|
53
|
+
private _elapsedMs = 0;
|
|
54
|
+
private _splits: Split[] = [];
|
|
55
|
+
private distanceAtSplitStart = 0;
|
|
56
|
+
private timeAtSplitStart = 0;
|
|
57
|
+
|
|
58
|
+
constructor(options: RecorderOptions = {}) {
|
|
59
|
+
this.filterOptions = options.filter ?? DEFAULT_FILTER_OPTIONS;
|
|
60
|
+
this.splitLengthMeters = options.splitLengthMeters ?? DEFAULT_SPLIT_LENGTH;
|
|
61
|
+
this.stopSpeedThresholdMps =
|
|
62
|
+
options.stopSpeedThresholdMps ?? DEFAULT_STOP_SPEED;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Offer a raw fix to the track. Returns true when the fix was accepted
|
|
67
|
+
* and recorded, false when a filter rule dropped it.
|
|
68
|
+
*/
|
|
69
|
+
addFix(fix: TrackFix): boolean {
|
|
70
|
+
const last = this.fixes[this.fixes.length - 1];
|
|
71
|
+
if (!shouldAcceptFix(fix, last, this.filterOptions)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (last) {
|
|
76
|
+
const segmentMeters = haversineMeters(last, fix);
|
|
77
|
+
const segmentMs = fix.timestamp - last.timestamp;
|
|
78
|
+
this._distanceMeters += segmentMeters;
|
|
79
|
+
this._elapsedMs += segmentMs;
|
|
80
|
+
|
|
81
|
+
const impliedSpeed =
|
|
82
|
+
segmentMs > 0 ? segmentMeters / (segmentMs / 1000) : 0;
|
|
83
|
+
const reportedSpeed = fix.speed >= 0 ? fix.speed : impliedSpeed;
|
|
84
|
+
if (reportedSpeed >= this.stopSpeedThresholdMps) {
|
|
85
|
+
this._movingMs += segmentMs;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
this._distanceMeters - this.distanceAtSplitStart >=
|
|
90
|
+
this.splitLengthMeters
|
|
91
|
+
) {
|
|
92
|
+
const splitDuration = fix.timestamp - this.timeAtSplitStart;
|
|
93
|
+
const splitDistance = this._distanceMeters - this.distanceAtSplitStart;
|
|
94
|
+
this._splits.push({
|
|
95
|
+
distanceMeters: splitDistance,
|
|
96
|
+
durationMs: splitDuration,
|
|
97
|
+
paceSecondsPerKm:
|
|
98
|
+
splitDistance > 0
|
|
99
|
+
? splitDuration / 1000 / (splitDistance / 1000)
|
|
100
|
+
: 0,
|
|
101
|
+
});
|
|
102
|
+
this.distanceAtSplitStart = this._distanceMeters;
|
|
103
|
+
this.timeAtSplitStart = fix.timestamp;
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
this.timeAtSplitStart = fix.timestamp;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.fixes.push(fix);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Accepted fixes, in order. */
|
|
114
|
+
get points(): TrackFix[] {
|
|
115
|
+
return [...this.fixes];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get distanceMeters(): number {
|
|
119
|
+
return this._distanceMeters;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Wall-clock time from first to last accepted fix, in milliseconds. */
|
|
123
|
+
get elapsedMs(): number {
|
|
124
|
+
return this._elapsedMs;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Time actually spent moving (stops excluded), in milliseconds. */
|
|
128
|
+
get movingMs(): number {
|
|
129
|
+
return this._movingMs;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Elapsed minus moving: time spent standing still, in milliseconds. */
|
|
133
|
+
get stoppedMs(): number {
|
|
134
|
+
return this._elapsedMs - this._movingMs;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** True when the most recent movement looks like a stop. */
|
|
138
|
+
get isStopped(): boolean {
|
|
139
|
+
if (this.fixes.length < 2) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
const last = this.fixes[this.fixes.length - 1];
|
|
143
|
+
return last !== undefined && last.speed >= 0
|
|
144
|
+
? last.speed < this.stopSpeedThresholdMps
|
|
145
|
+
: false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Completed splits so far. */
|
|
149
|
+
get splits(): Split[] {
|
|
150
|
+
return [...this._splits];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Average pace over the whole track, seconds per kilometer. */
|
|
154
|
+
get averagePaceSecondsPerKm(): number {
|
|
155
|
+
if (this._distanceMeters === 0 || this._movingMs === 0) {
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
return this._movingMs / 1000 / (this._distanceMeters / 1000);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
reset(): void {
|
|
162
|
+
this.fixes = [];
|
|
163
|
+
this._distanceMeters = 0;
|
|
164
|
+
this._movingMs = 0;
|
|
165
|
+
this._elapsedMs = 0;
|
|
166
|
+
this._splits = [];
|
|
167
|
+
this.distanceAtSplitStart = 0;
|
|
168
|
+
this.timeAtSplitStart = 0;
|
|
169
|
+
}
|
|
170
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
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 RecorderOptions,
|
|
32
|
+
} from './geo/recorder';
|
|
33
|
+
|
|
34
|
+
type ListenerSubscription = ReturnType<
|
|
35
|
+
typeof NativeBackgroundGeolocation.onLocation
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Start background location tracking.
|
|
40
|
+
*
|
|
41
|
+
* The app is responsible for holding location permission (and, for true
|
|
42
|
+
* background tracking, the background location permission) before calling
|
|
43
|
+
* this. The promise rejects with code E_PERMISSION when it does not.
|
|
44
|
+
*/
|
|
45
|
+
export function start(config: GeolocationConfig = {}): Promise<void> {
|
|
46
|
+
return NativeBackgroundGeolocation.start(config);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Stop tracking and release the native engine. */
|
|
50
|
+
export function stop(): Promise<void> {
|
|
51
|
+
return NativeBackgroundGeolocation.stop();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* One-shot position query: ask the OS where the device is right now,
|
|
56
|
+
* without starting continuous tracking.
|
|
57
|
+
*/
|
|
58
|
+
export function getCurrentPosition(): Promise<LocationFix> {
|
|
59
|
+
return NativeBackgroundGeolocation.getCurrentPosition();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Whether native tracking is currently running. */
|
|
63
|
+
export function isTracking(): Promise<boolean> {
|
|
64
|
+
return NativeBackgroundGeolocation.isTracking();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Subscribe to fixes while tracking. Returns a subscription to remove.
|
|
69
|
+
*
|
|
70
|
+
* With codegen EventEmitter properties, the spec property itself is the
|
|
71
|
+
* subscribe function. Do NOT route this through NativeEventEmitter: its
|
|
72
|
+
* legacy addListener call throws inside the new-architecture TurboModule
|
|
73
|
+
* invocation layer and aborts the app (found the hard way, build 7).
|
|
74
|
+
*/
|
|
75
|
+
export function addLocationListener(
|
|
76
|
+
listener: (fix: LocationFix) => void
|
|
77
|
+
): ListenerSubscription {
|
|
78
|
+
return NativeBackgroundGeolocation.onLocation(listener);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Subscribe to native engine errors. Returns a subscription to remove. */
|
|
82
|
+
export function addErrorListener(
|
|
83
|
+
listener: (error: GeolocationError) => void
|
|
84
|
+
): ListenerSubscription {
|
|
85
|
+
return NativeBackgroundGeolocation.onError(listener);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default {
|
|
89
|
+
start,
|
|
90
|
+
stop,
|
|
91
|
+
getCurrentPosition,
|
|
92
|
+
isTracking,
|
|
93
|
+
addLocationListener,
|
|
94
|
+
addErrorListener,
|
|
95
|
+
};
|
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
|
+
}
|