@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.
Files changed (37) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +129 -2
  3. package/ReactNativeBackgroundGeolocation.podspec +22 -0
  4. package/android/build.gradle +36 -0
  5. package/android/src/main/AndroidManifest.xml +9 -0
  6. package/android/src/main/java/com/pakeku/reactnativebackgroundgeolocation/ReactNativeBackgroundGeolocationModule.kt +185 -0
  7. package/android/src/main/java/com/pakeku/reactnativebackgroundgeolocation/ReactNativeBackgroundGeolocationPackage.kt +30 -0
  8. package/ios/ReactNativeBackgroundGeolocation.h +5 -0
  9. package/ios/ReactNativeBackgroundGeolocation.mm +147 -0
  10. package/lib/module/NativeReactNativeBackgroundGeolocation.js +15 -0
  11. package/lib/module/NativeReactNativeBackgroundGeolocation.js.map +1 -0
  12. package/lib/module/geo/filter.js +37 -0
  13. package/lib/module/geo/filter.js.map +1 -0
  14. package/lib/module/geo/recorder.js +278 -0
  15. package/lib/module/geo/recorder.js.map +1 -0
  16. package/lib/module/index.js +60 -0
  17. package/lib/module/index.js.map +1 -0
  18. package/lib/module/location.js +71 -0
  19. package/lib/module/location.js.map +1 -0
  20. package/lib/module/package.json +1 -0
  21. package/lib/typescript/package.json +1 -0
  22. package/lib/typescript/src/NativeReactNativeBackgroundGeolocation.d.ts +66 -0
  23. package/lib/typescript/src/NativeReactNativeBackgroundGeolocation.d.ts.map +1 -0
  24. package/lib/typescript/src/geo/filter.d.ts +26 -0
  25. package/lib/typescript/src/geo/filter.d.ts.map +1 -0
  26. package/lib/typescript/src/geo/recorder.d.ts +132 -0
  27. package/lib/typescript/src/geo/recorder.d.ts.map +1 -0
  28. package/lib/typescript/src/index.d.ts +44 -0
  29. package/lib/typescript/src/index.d.ts.map +1 -0
  30. package/lib/typescript/src/location.d.ts +35 -0
  31. package/lib/typescript/src/location.d.ts.map +1 -0
  32. package/package.json +144 -4
  33. package/src/NativeReactNativeBackgroundGeolocation.ts +73 -0
  34. package/src/geo/filter.ts +62 -0
  35. package/src/geo/recorder.ts +395 -0
  36. package/src/index.tsx +97 -0
  37. package/src/location.ts +93 -0
@@ -0,0 +1,132 @@
1
+ import { type FilterOptions, type FixLike } from './filter.js';
2
+ /**
3
+ * TrackRecorder ingests raw fixes and builds a clean track: total distance,
4
+ * elapsed and moving time, per-kilometer (or per-mile) splits, and stop
5
+ * detection. It is the "Strava brain" of the library, written as pure
6
+ * TypeScript so it can be proven with unit tests and reused by any app
7
+ * built on the native engine.
8
+ */
9
+ export type TrackFix = FixLike & {
10
+ altitude: number;
11
+ speed: number;
12
+ };
13
+ export type Split = {
14
+ /** Split length in meters (1000 unless a custom length was configured). */
15
+ distanceMeters: number;
16
+ /** Duration of this split in milliseconds. */
17
+ durationMs: number;
18
+ /** Average pace for the split, seconds per kilometer. */
19
+ paceSecondsPerKm: number;
20
+ };
21
+ /**
22
+ * One continuous stretch of a track, bounded by pauses. When the recorder
23
+ * stays stopped for at least `pauseThresholdMs`, the current segment is
24
+ * closed at the last moving fix and the next moving fix opens a new one.
25
+ * The pause itself belongs to no segment.
26
+ */
27
+ export type TrackSegment = {
28
+ /** Timestamp of the segment's first fix. */
29
+ startTimestamp: number;
30
+ /** Timestamp of the segment's last fix. */
31
+ endTimestamp: number;
32
+ /** endTimestamp - startTimestamp, in milliseconds. */
33
+ durationMs: number;
34
+ /** Distance covered inside the segment, in meters. */
35
+ distanceMeters: number;
36
+ /** Time spent moving inside the segment, in milliseconds. */
37
+ movingMs: number;
38
+ /** Accepted fixes in the segment. */
39
+ fixCount: number;
40
+ };
41
+ /** Everything an app needs to render a finished (or live) activity. */
42
+ export type TripSummary = {
43
+ distanceMeters: number;
44
+ elapsedMs: number;
45
+ movingMs: number;
46
+ stoppedMs: number;
47
+ /** Sum of positive altitude changes, in meters. Descents are ignored. */
48
+ elevationGainMeters: number;
49
+ /** Moving-time pace, seconds per kilometer. 0 when undefined. */
50
+ averagePaceSecondsPerKm: number;
51
+ fixCount: number;
52
+ segmentCount: number;
53
+ segments: TrackSegment[];
54
+ splits: Split[];
55
+ };
56
+ export type RecorderOptions = {
57
+ filter?: FilterOptions;
58
+ /** Length of each split in meters. Default 1000 (per-kilometer splits). */
59
+ splitLengthMeters?: number;
60
+ /**
61
+ * A fix counts as "stopped" when its speed (or implied speed from the
62
+ * previous fix) stays below this, in meters per second. Default 0.5.
63
+ */
64
+ stopSpeedThresholdMps?: number;
65
+ /**
66
+ * A stop lasting at least this long ends the current segment; the next
67
+ * moving fix starts a new one. Default 180000 (3 minutes).
68
+ */
69
+ pauseThresholdMs?: number;
70
+ };
71
+ export declare class TrackRecorder {
72
+ private readonly filterOptions;
73
+ private readonly splitLengthMeters;
74
+ private readonly stopSpeedThresholdMps;
75
+ private readonly pauseThresholdMs;
76
+ private fixes;
77
+ private _distanceMeters;
78
+ private _movingMs;
79
+ private _elapsedMs;
80
+ private _elevationGainMeters;
81
+ private _splits;
82
+ private distanceAtSplitStart;
83
+ private timeAtSplitStart;
84
+ private completedSegments;
85
+ private segment;
86
+ /** Stopped time accumulated in the current, not-yet-a-pause stop. */
87
+ private stopEpisodeMs;
88
+ /**
89
+ * Segment state captured when the current stop began. If the stop turns
90
+ * into a pause, the segment is closed from this snapshot, so the stopped
91
+ * fixes never join the segment they interrupted.
92
+ */
93
+ private stopSnapshot;
94
+ constructor(options?: RecorderOptions);
95
+ /**
96
+ * Offer a raw fix to the track. Returns true when the fix was accepted
97
+ * and recorded, false when a filter rule dropped it.
98
+ */
99
+ addFix(fix: TrackFix): boolean;
100
+ /**
101
+ * Fold one accepted pair (previous fix -> this fix) into the segment
102
+ * bookkeeping, closing and reopening segments around pauses.
103
+ */
104
+ private recordSegmentPair;
105
+ /** Accepted fixes, in order. */
106
+ get points(): TrackFix[];
107
+ get distanceMeters(): number;
108
+ /** Wall-clock time from first to last accepted fix, in milliseconds. */
109
+ get elapsedMs(): number;
110
+ /** Time actually spent moving (stops excluded), in milliseconds. */
111
+ get movingMs(): number;
112
+ /** Elapsed minus moving: time spent standing still, in milliseconds. */
113
+ get stoppedMs(): number;
114
+ /** True when the most recent movement looks like a stop. */
115
+ get isStopped(): boolean;
116
+ /** Completed splits so far. */
117
+ get splits(): Split[];
118
+ /** Total climbing, in meters: positive altitude changes only. */
119
+ get elevationGainMeters(): number;
120
+ /**
121
+ * All segments, finished ones plus the one still in progress. When the
122
+ * recorder is sitting in a pause that has passed the threshold, the
123
+ * interrupted segment is already shown closed at its last moving fix.
124
+ */
125
+ get segments(): TrackSegment[];
126
+ /** One-call snapshot of the whole track, for rendering an activity. */
127
+ summary(): TripSummary;
128
+ /** Average pace over the whole track, seconds per kilometer. */
129
+ get averagePaceSecondsPerKm(): number;
130
+ reset(): void;
131
+ }
132
+ //# sourceMappingURL=recorder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../src/geo/recorder.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,44 @@
1
+ import NativeBackgroundGeolocation, { type GeolocationConfig, type GeolocationError, type LocationFix } from './NativeReactNativeBackgroundGeolocation.js';
2
+ export type { GeolocationConfig, GeolocationError, LocationFix };
3
+ export { haversineMeters, totalDistanceMeters, filterByAccuracy, durationSeconds, averagePaceMinPerKm, type Coordinate, type GeoFix, } from './location.js';
4
+ export { shouldAcceptFix, hasAcceptableAccuracy, isChronological, isFarEnough, DEFAULT_FILTER_OPTIONS, type FilterOptions, type FixLike, } from './geo/filter.js';
5
+ export { TrackRecorder, type TrackFix, type Split, type TrackSegment, type TripSummary, type RecorderOptions, } from './geo/recorder.js';
6
+ type ListenerSubscription = ReturnType<typeof NativeBackgroundGeolocation.onLocation>;
7
+ /**
8
+ * Start background location tracking.
9
+ *
10
+ * The app is responsible for holding location permission (and, for true
11
+ * background tracking, the background location permission) before calling
12
+ * this. The promise rejects with code E_PERMISSION when it does not.
13
+ */
14
+ export declare function start(config?: GeolocationConfig): Promise<void>;
15
+ /** Stop tracking and release the native engine. */
16
+ export declare function stop(): Promise<void>;
17
+ /**
18
+ * One-shot position query: ask the OS where the device is right now,
19
+ * without starting continuous tracking.
20
+ */
21
+ export declare function getCurrentPosition(): Promise<LocationFix>;
22
+ /** Whether native tracking is currently running. */
23
+ export declare function isTracking(): Promise<boolean>;
24
+ /**
25
+ * Subscribe to fixes while tracking. Returns a subscription to remove.
26
+ *
27
+ * With codegen EventEmitter properties, the spec property itself is the
28
+ * subscribe function. Do NOT route this through NativeEventEmitter: its
29
+ * legacy addListener call throws inside the new-architecture TurboModule
30
+ * invocation layer and aborts the app (found the hard way, build 7).
31
+ */
32
+ export declare function addLocationListener(listener: (fix: LocationFix) => void): ListenerSubscription;
33
+ /** Subscribe to native engine errors. Returns a subscription to remove. */
34
+ export declare function addErrorListener(listener: (error: GeolocationError) => void): ListenerSubscription;
35
+ declare const _default: {
36
+ start: typeof start;
37
+ stop: typeof stop;
38
+ getCurrentPosition: typeof getCurrentPosition;
39
+ isTracking: typeof isTracking;
40
+ addLocationListener: typeof addLocationListener;
41
+ addErrorListener: typeof addErrorListener;
42
+ };
43
+ export default _default;
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,EAAE,EAClC,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EACjB,MAAM,6CAA0C,CAAC;AAElD,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,WAAW,EAAE,CAAC;AAEjE,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,KAAK,UAAU,EACf,KAAK,MAAM,GACZ,MAAM,eAAY,CAAC;AACpB,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,WAAW,EACX,sBAAsB,EACtB,KAAK,aAAa,EAClB,KAAK,OAAO,GACb,MAAM,iBAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,KAAK,QAAQ,EACb,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,GACrB,MAAM,mBAAgB,CAAC;AAExB,KAAK,oBAAoB,GAAG,UAAU,CACpC,OAAO,2BAA2B,CAAC,UAAU,CAC9C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,MAAM,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAEnE;AAED,mDAAmD;AACnD,wBAAgB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAEpC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAAC,WAAW,CAAC,CAEzD;AAED,oDAAoD;AACpD,wBAAgB,UAAU,IAAI,OAAO,CAAC,OAAO,CAAC,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,IAAI,GACnC,oBAAoB,CAEtB;AAED,2EAA2E;AAC3E,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAC1C,oBAAoB,CAEtB;;;;;;;;;AAED,wBAOE"}
@@ -0,0 +1,35 @@
1
+ export interface GeoFix {
2
+ latitude: number;
3
+ longitude: number;
4
+ /** Unix epoch milliseconds, from the OS location timestamp. */
5
+ timestampMs: number;
6
+ /** Metres above sea level, when the OS reports it. */
7
+ altitude?: number;
8
+ /** Horizontal accuracy in metres, when the OS reports it. Lower is better. */
9
+ accuracy?: number;
10
+ /** Speed in m/s, when the OS reports it. */
11
+ speed?: number;
12
+ }
13
+ /** Anything with a latitude and a longitude: a fix, a waypoint, a pin. */
14
+ export type Coordinate = {
15
+ latitude: number;
16
+ longitude: number;
17
+ };
18
+ /** Great-circle distance between two coordinates, in metres. */
19
+ export declare function haversineMeters(a: Coordinate, b: Coordinate): number;
20
+ /** Sum of haversine distances between consecutive points, in metres. */
21
+ export declare function totalDistanceMeters(fixes: readonly Coordinate[]): number;
22
+ /**
23
+ * Drop fixes whose reported accuracy is worse than `maxAccuracyM`.
24
+ * Fixes with no accuracy reading are kept: the OS gave us no reason to
25
+ * distrust them. Default 50 m matches a typical running-track tolerance.
26
+ */
27
+ export declare function filterByAccuracy(fixes: readonly GeoFix[], maxAccuracyM?: number): GeoFix[];
28
+ /** Elapsed time between the first and last fix, in seconds. */
29
+ export declare function durationSeconds(fixes: readonly GeoFix[]): number;
30
+ /**
31
+ * Average pace in minutes per kilometre. Returns null when there is no
32
+ * usable distance or duration (avoids a divide-by-zero pace of Infinity).
33
+ */
34
+ export declare function averagePaceMinPerKm(fixes: readonly GeoFix[]): number | null;
35
+ //# sourceMappingURL=location.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"location.d.ts","sourceRoot":"","sources":["../../../src/location.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IACpB,sDAAsD;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8EAA8E;IAC9E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4CAA4C;IAC5C,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAQD,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,MAAM,CASpE;AAED,wEAAwE;AACxE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,CAUxE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,YAAY,SAAK,GAChB,MAAM,EAAE,CAIV;AAED,+DAA+D;AAC/D,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAOhE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAO3E"}
package/package.json CHANGED
@@ -1,6 +1,146 @@
1
1
  {
2
2
  "name": "@nobody-os/react-native-background-geolocation",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
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
+ }