@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/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Erick Pacheco
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,128 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nobody-os/react-native-background-geolocation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Open-source background geolocation for React Native: continuous background
|
|
4
|
+
location tracking for iOS and Android, MIT licensed.
|
|
5
|
+
|
|
6
|
+
The React Native ecosystem's serious background-location library is
|
|
7
|
+
commercial. This one is not, and never will be: MIT, modern (TurboModules,
|
|
8
|
+
the new architecture), TypeScript-first, and tested. It is built and
|
|
9
|
+
dogfooded by a runner, for anyone building run, walk, hike, or delivery
|
|
10
|
+
tracking into a React Native app.
|
|
11
|
+
|
|
12
|
+
## Status
|
|
13
|
+
|
|
14
|
+
v0.1: the contract, the engines, and the brains.
|
|
15
|
+
|
|
16
|
+
- **Contract:** a typed TurboModule spec (`start`, `stop`,
|
|
17
|
+
`getCurrentPosition`, `isTracking`) with `onLocation` / `onError` events,
|
|
18
|
+
codegen-verified for both platforms.
|
|
19
|
+
- **Engines:** iOS on `CLLocationManager` (background updates enabled),
|
|
20
|
+
Android on the platform `LocationManager` (no Play Services dependency).
|
|
21
|
+
- **Brains:** a pure TypeScript layer any app can use on top of the fix
|
|
22
|
+
stream: haversine distance, GPS fix filtering (accuracy, jitter,
|
|
23
|
+
out-of-order), and a `TrackRecorder` that builds distance, moving vs
|
|
24
|
+
stopped time, and per-kilometer splits. This layer is fully unit-tested
|
|
25
|
+
and runs anywhere, including CI on a machine with no phone attached.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm install @nobody-os/react-native-background-geolocation
|
|
31
|
+
# or
|
|
32
|
+
yarn add @nobody-os/react-native-background-geolocation
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Permissions
|
|
36
|
+
|
|
37
|
+
Your app owns the permission prompts; the library owns the tracking.
|
|
38
|
+
|
|
39
|
+
- **Android:** the library's manifest declares fine/coarse/background
|
|
40
|
+
location and foreground-service permissions. Request
|
|
41
|
+
`ACCESS_FINE_LOCATION` at runtime (and background location separately)
|
|
42
|
+
before calling `start`.
|
|
43
|
+
- **iOS:** add `NSLocationWhenInUseUsageDescription` and
|
|
44
|
+
`NSLocationAlwaysAndWhenInUseUsageDescription` to your Info.plist and
|
|
45
|
+
enable the `location` background mode. The example app shows both.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import BackgroundGeolocation, {
|
|
51
|
+
TrackRecorder,
|
|
52
|
+
} from '@nobody-os/react-native-background-geolocation';
|
|
53
|
+
|
|
54
|
+
const recorder = new TrackRecorder();
|
|
55
|
+
|
|
56
|
+
const subscription = BackgroundGeolocation.addLocationListener((fix) => {
|
|
57
|
+
recorder.addFix(fix);
|
|
58
|
+
console.log(
|
|
59
|
+
`${(recorder.distanceMeters / 1000).toFixed(2)} km`,
|
|
60
|
+
`pace ${recorder.averagePaceSecondsPerKm.toFixed(0)} s/km`
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
await BackgroundGeolocation.start({
|
|
65
|
+
desiredAccuracy: 'high', // GPS, for run/walk tracking
|
|
66
|
+
distanceFilter: 10, // meters between fixes
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Later, when the workout ends:
|
|
70
|
+
await BackgroundGeolocation.stop();
|
|
71
|
+
subscription.remove();
|
|
72
|
+
|
|
73
|
+
// One-shot query, no tracking:
|
|
74
|
+
const fix = await BackgroundGeolocation.getCurrentPosition();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API
|
|
78
|
+
|
|
79
|
+
| Function | Description |
|
|
80
|
+
| --- | --- |
|
|
81
|
+
| `start(config)` | Start background tracking. Config: `desiredAccuracy` (`'high'` / `'balanced'` / `'low'`), `distanceFilter` (m), `interval` / `fastestInterval` (ms, Android), `stopTimeout` (min), `debug`. |
|
|
82
|
+
| `stop()` | Stop tracking and release the native engine. |
|
|
83
|
+
| `getCurrentPosition()` | One-shot position query: ask the OS where the device is right now. |
|
|
84
|
+
| `isTracking()` | Whether native tracking is currently running. |
|
|
85
|
+
| `addLocationListener(cb)` | Subscribe to fixes while tracking. Returns a subscription with `.remove()`. |
|
|
86
|
+
| `addErrorListener(cb)` | Subscribe to native errors (`code` 1 = permission denied, 2 = position unavailable, 3 = timeout). |
|
|
87
|
+
|
|
88
|
+
Plus the pure layer, exported from the same package: `haversineMeters`,
|
|
89
|
+
`totalDistanceMeters`, `filterByAccuracy`, `shouldAcceptFix`,
|
|
90
|
+
`TrackRecorder`, and their types.
|
|
91
|
+
|
|
92
|
+
## Roadmap
|
|
93
|
+
|
|
94
|
+
- **v0.2:** Android foreground service, so tracking survives the app being
|
|
95
|
+
swiped away, with a configurable notification.
|
|
96
|
+
- **v0.2:** FusedLocationProvider engine option on Android.
|
|
97
|
+
- **v0.3:** Activity-aware sampling (still / walking / running) and stop
|
|
98
|
+
detection driving the native engines, for battery life.
|
|
99
|
+
- **v0.3:** Offline fix queue with batch sync hooks.
|
|
100
|
+
- Geofencing, GPX import/export helpers, and a MapLibre-based example
|
|
101
|
+
with live route drawing.
|
|
102
|
+
|
|
103
|
+
## Development
|
|
104
|
+
|
|
105
|
+
```sh
|
|
106
|
+
yarn # install dependencies
|
|
107
|
+
yarn test # unit tests (pure TypeScript, no phone needed)
|
|
108
|
+
yarn typecheck
|
|
109
|
+
yarn lint
|
|
110
|
+
yarn prepare # build the library with bob
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The example app (`example/`) is the test bench: it requests permission,
|
|
114
|
+
tracks, and shows live distance from the `TrackRecorder`.
|
|
115
|
+
|
|
116
|
+
## Contributing
|
|
117
|
+
|
|
118
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
119
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
120
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
MIT
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "ReactNativeBackgroundGeolocation"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/pakeku/react-native-background-geolocation.git", :tag => "v#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift,cpp}"
|
|
17
|
+
s.private_header_files = "ios/**/*.h"
|
|
18
|
+
|
|
19
|
+
s.frameworks = "CoreLocation"
|
|
20
|
+
|
|
21
|
+
install_modules_dependencies(s)
|
|
22
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
ext.ReactNativeBackgroundGeolocation = [
|
|
2
|
+
minSdkVersion: 24,
|
|
3
|
+
compileSdkVersion: 36
|
|
4
|
+
]
|
|
5
|
+
|
|
6
|
+
ext.getExtOrDefault = { prop ->
|
|
7
|
+
if (rootProject.ext.has(prop)) {
|
|
8
|
+
return rootProject.ext.get(prop)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return ReactNativeBackgroundGeolocation[prop]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
apply plugin: "com.android.library"
|
|
16
|
+
|
|
17
|
+
if (project.extensions.findByName("kotlin") == null) {
|
|
18
|
+
apply plugin: "kotlin-android"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
apply plugin: "com.facebook.react"
|
|
23
|
+
|
|
24
|
+
android {
|
|
25
|
+
namespace "com.pakeku.reactnativebackgroundgeolocation"
|
|
26
|
+
|
|
27
|
+
compileSdkVersion getExtOrDefault("compileSdkVersion")
|
|
28
|
+
|
|
29
|
+
defaultConfig {
|
|
30
|
+
minSdkVersion getExtOrDefault("minSdkVersion")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
dependencies {
|
|
35
|
+
implementation "com.facebook.react:react-android"
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
|
|
3
|
+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
|
4
|
+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
|
5
|
+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
|
6
|
+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
7
|
+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
|
|
8
|
+
|
|
9
|
+
</manifest>
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
package com.pakeku.reactnativebackgroundgeolocation
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.location.Location
|
|
6
|
+
import android.location.LocationListener
|
|
7
|
+
import android.location.LocationManager
|
|
8
|
+
import android.os.Bundle
|
|
9
|
+
import android.os.Looper
|
|
10
|
+
import com.facebook.react.bridge.Arguments
|
|
11
|
+
import com.facebook.react.bridge.Promise
|
|
12
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
13
|
+
import com.facebook.react.bridge.ReadableMap
|
|
14
|
+
import com.facebook.react.bridge.WritableMap
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Android engine for the background geolocation module.
|
|
18
|
+
*
|
|
19
|
+
* v0.1 uses the platform LocationManager (no Play Services dependency).
|
|
20
|
+
* Fixes are pushed to JS through the codegen event emitters
|
|
21
|
+
* (emitOnLocation / emitOnError) declared in the TurboModule spec.
|
|
22
|
+
* A foreground service so tracking survives the app being swiped away
|
|
23
|
+
* is the headline v0.2 item; see the README roadmap.
|
|
24
|
+
*/
|
|
25
|
+
class ReactNativeBackgroundGeolocationModule(reactContext: ReactApplicationContext) :
|
|
26
|
+
NativeReactNativeBackgroundGeolocationSpec(reactContext), LocationListener {
|
|
27
|
+
|
|
28
|
+
private val locationManager: LocationManager? =
|
|
29
|
+
reactContext.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
|
|
30
|
+
|
|
31
|
+
@Volatile
|
|
32
|
+
private var tracking = false
|
|
33
|
+
|
|
34
|
+
private var pendingPromise: Promise? = null
|
|
35
|
+
private var singleShot = false
|
|
36
|
+
|
|
37
|
+
@SuppressLint("MissingPermission")
|
|
38
|
+
override fun start(config: ReadableMap, promise: Promise) {
|
|
39
|
+
val manager = locationManager
|
|
40
|
+
if (manager == null) {
|
|
41
|
+
promise.reject("E_UNAVAILABLE", "LocationManager is not available on this device")
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
val provider = pickProvider(manager, config)
|
|
45
|
+
if (provider == null) {
|
|
46
|
+
promise.reject("E_UNAVAILABLE", "No location provider is enabled")
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
val intervalMs =
|
|
50
|
+
if (config.hasKey("interval")) config.getDouble("interval").toLong() else 5_000L
|
|
51
|
+
val distanceFilter =
|
|
52
|
+
if (config.hasKey("distanceFilter")) config.getDouble("distanceFilter").toFloat() else 10f
|
|
53
|
+
try {
|
|
54
|
+
manager.requestLocationUpdates(
|
|
55
|
+
provider,
|
|
56
|
+
intervalMs,
|
|
57
|
+
distanceFilter,
|
|
58
|
+
this,
|
|
59
|
+
Looper.getMainLooper(),
|
|
60
|
+
)
|
|
61
|
+
tracking = true
|
|
62
|
+
promise.resolve(null)
|
|
63
|
+
} catch (e: SecurityException) {
|
|
64
|
+
emitError(1, "Location permission not granted")
|
|
65
|
+
promise.reject("E_PERMISSION", "Location permission not granted", e)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
override fun stop(promise: Promise) {
|
|
70
|
+
locationManager?.removeUpdates(this)
|
|
71
|
+
tracking = false
|
|
72
|
+
singleShot = false
|
|
73
|
+
pendingPromise = null
|
|
74
|
+
promise.resolve(null)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@SuppressLint("MissingPermission")
|
|
78
|
+
override fun getCurrentPosition(promise: Promise) {
|
|
79
|
+
val manager = locationManager
|
|
80
|
+
if (manager == null) {
|
|
81
|
+
promise.reject("E_UNAVAILABLE", "LocationManager is not available on this device")
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
val lastKnown = lastKnownFix(manager)
|
|
85
|
+
if (lastKnown != null && System.currentTimeMillis() - lastKnown.time < FRESH_FIX_MS) {
|
|
86
|
+
promise.resolve(fixToMap(lastKnown))
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
val provider = pickProvider(manager, null)
|
|
90
|
+
if (provider == null) {
|
|
91
|
+
promise.reject("E_UNAVAILABLE", "No location provider is enabled")
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
pendingPromise = promise
|
|
96
|
+
singleShot = true
|
|
97
|
+
manager.requestLocationUpdates(provider, 0L, 0f, this, Looper.getMainLooper())
|
|
98
|
+
} catch (e: SecurityException) {
|
|
99
|
+
pendingPromise = null
|
|
100
|
+
singleShot = false
|
|
101
|
+
promise.reject("E_PERMISSION", "Location permission not granted", e)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
override fun isTracking(promise: Promise) {
|
|
106
|
+
promise.resolve(tracking)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
override fun onLocationChanged(location: Location) {
|
|
110
|
+
val map = fixToMap(location)
|
|
111
|
+
pendingPromise?.let {
|
|
112
|
+
it.resolve(map)
|
|
113
|
+
pendingPromise = null
|
|
114
|
+
if (singleShot && !tracking) {
|
|
115
|
+
locationManager?.removeUpdates(this)
|
|
116
|
+
}
|
|
117
|
+
singleShot = false
|
|
118
|
+
}
|
|
119
|
+
if (tracking) {
|
|
120
|
+
emitOnLocation(map)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
override fun onProviderEnabled(provider: String) = Unit
|
|
125
|
+
|
|
126
|
+
override fun onProviderDisabled(provider: String) {
|
|
127
|
+
emitError(2, "Location provider disabled: $provider")
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@Deprecated("Deprecated in the Android SDK; required by the LocationListener interface")
|
|
131
|
+
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) = Unit
|
|
132
|
+
|
|
133
|
+
override fun invalidate() {
|
|
134
|
+
locationManager?.removeUpdates(this)
|
|
135
|
+
tracking = false
|
|
136
|
+
super.invalidate()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private fun pickProvider(manager: LocationManager, config: ReadableMap?): String? {
|
|
140
|
+
val accuracy = config?.let { if (it.hasKey("desiredAccuracy")) it.getString("desiredAccuracy") else null }
|
|
141
|
+
val preferred =
|
|
142
|
+
if (accuracy == "low") LocationManager.NETWORK_PROVIDER else LocationManager.GPS_PROVIDER
|
|
143
|
+
val fallback =
|
|
144
|
+
if (preferred == LocationManager.GPS_PROVIDER) {
|
|
145
|
+
LocationManager.NETWORK_PROVIDER
|
|
146
|
+
} else {
|
|
147
|
+
LocationManager.GPS_PROVIDER
|
|
148
|
+
}
|
|
149
|
+
return when {
|
|
150
|
+
manager.isProviderEnabled(preferred) -> preferred
|
|
151
|
+
manager.isProviderEnabled(fallback) -> fallback
|
|
152
|
+
else -> null
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private fun lastKnownFix(manager: LocationManager): Location? {
|
|
157
|
+
val gps = try { manager.getLastKnownLocation(LocationManager.GPS_PROVIDER) } catch (e: SecurityException) { null }
|
|
158
|
+
val network = try { manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) } catch (e: SecurityException) { null }
|
|
159
|
+
return listOfNotNull(gps, network).maxByOrNull { it.time }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private fun fixToMap(location: Location): WritableMap {
|
|
163
|
+
val map = Arguments.createMap()
|
|
164
|
+
map.putDouble("latitude", location.latitude)
|
|
165
|
+
map.putDouble("longitude", location.longitude)
|
|
166
|
+
map.putDouble("accuracy", location.accuracy.toDouble())
|
|
167
|
+
map.putDouble("altitude", if (location.hasAltitude()) location.altitude else -1.0)
|
|
168
|
+
map.putDouble("speed", if (location.hasSpeed()) location.speed.toDouble() else -1.0)
|
|
169
|
+
map.putDouble("heading", if (location.hasBearing()) location.bearing.toDouble() else -1.0)
|
|
170
|
+
map.putDouble("timestamp", location.time.toDouble())
|
|
171
|
+
return map
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private fun emitError(code: Int, message: String) {
|
|
175
|
+
val map = Arguments.createMap()
|
|
176
|
+
map.putInt("code", code)
|
|
177
|
+
map.putString("message", message)
|
|
178
|
+
emitOnError(map)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
companion object {
|
|
182
|
+
const val NAME = NativeReactNativeBackgroundGeolocationSpec.NAME
|
|
183
|
+
private const val FRESH_FIX_MS = 60_000L
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package com.pakeku.reactnativebackgroundgeolocation
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
class ReactNativeBackgroundGeolocationPackage : BaseReactPackage() {
|
|
10
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
11
|
+
return if (name == ReactNativeBackgroundGeolocationModule.NAME) {
|
|
12
|
+
ReactNativeBackgroundGeolocationModule(reactContext)
|
|
13
|
+
} else {
|
|
14
|
+
null
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
|
|
19
|
+
mapOf(
|
|
20
|
+
ReactNativeBackgroundGeolocationModule.NAME to ReactModuleInfo(
|
|
21
|
+
name = ReactNativeBackgroundGeolocationModule.NAME,
|
|
22
|
+
className = ReactNativeBackgroundGeolocationModule.NAME,
|
|
23
|
+
canOverrideExistingModule = false,
|
|
24
|
+
needsEagerInit = false,
|
|
25
|
+
isCxxModule = false,
|
|
26
|
+
isTurboModule = true
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#import "ReactNativeBackgroundGeolocation.h"
|
|
2
|
+
#import <CoreLocation/CoreLocation.h>
|
|
3
|
+
|
|
4
|
+
@interface ReactNativeBackgroundGeolocation () <CLLocationManagerDelegate>
|
|
5
|
+
@property (nonatomic, strong) CLLocationManager *locationManager;
|
|
6
|
+
@property (nonatomic, assign) BOOL tracking;
|
|
7
|
+
@property (nonatomic, copy, nullable) RCTPromiseResolveBlock pendingResolve;
|
|
8
|
+
@property (nonatomic, copy, nullable) RCTPromiseRejectBlock pendingReject;
|
|
9
|
+
@end
|
|
10
|
+
|
|
11
|
+
@implementation ReactNativeBackgroundGeolocation
|
|
12
|
+
|
|
13
|
+
+ (BOOL)requiresMainQueueSetup
|
|
14
|
+
{
|
|
15
|
+
return YES;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
- (CLLocationManager *)locationManager
|
|
19
|
+
{
|
|
20
|
+
if (!_locationManager) {
|
|
21
|
+
_locationManager = [CLLocationManager new];
|
|
22
|
+
_locationManager.delegate = self;
|
|
23
|
+
_locationManager.allowsBackgroundLocationUpdates = YES;
|
|
24
|
+
_locationManager.pausesLocationUpdatesAutomatically = NO;
|
|
25
|
+
}
|
|
26
|
+
return _locationManager;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
- (NSDictionary *)dictionaryFromLocation:(CLLocation *)location
|
|
30
|
+
{
|
|
31
|
+
return @{
|
|
32
|
+
@"latitude" : @(location.coordinate.latitude),
|
|
33
|
+
@"longitude" : @(location.coordinate.longitude),
|
|
34
|
+
@"accuracy" : @(location.horizontalAccuracy),
|
|
35
|
+
@"altitude" : @(location.altitude),
|
|
36
|
+
@"speed" : @(location.speed),
|
|
37
|
+
@"heading" : @(location.course),
|
|
38
|
+
@"timestamp" : @([location.timestamp timeIntervalSince1970] * 1000),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
- (void)start:(JS::NativeReactNativeBackgroundGeolocation::GeolocationConfig &)config
|
|
43
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
44
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
45
|
+
{
|
|
46
|
+
CLLocationManager *manager = self.locationManager;
|
|
47
|
+
|
|
48
|
+
NSString *profile = config.desiredAccuracy();
|
|
49
|
+
if ([profile isEqualToString:@"high"]) {
|
|
50
|
+
manager.desiredAccuracy = kCLLocationAccuracyBest;
|
|
51
|
+
} else if ([profile isEqualToString:@"low"]) {
|
|
52
|
+
manager.desiredAccuracy = kCLLocationAccuracyKilometer;
|
|
53
|
+
} else {
|
|
54
|
+
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
auto distanceFilter = config.distanceFilter();
|
|
58
|
+
manager.distanceFilter = distanceFilter.has_value() ? distanceFilter.value() : 10;
|
|
59
|
+
|
|
60
|
+
[manager requestAlwaysAuthorization];
|
|
61
|
+
[manager startUpdatingLocation];
|
|
62
|
+
self.tracking = YES;
|
|
63
|
+
resolve(nil);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
- (void)stop:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
67
|
+
{
|
|
68
|
+
[self.locationManager stopUpdatingLocation];
|
|
69
|
+
self.tracking = NO;
|
|
70
|
+
self.pendingResolve = nil;
|
|
71
|
+
self.pendingReject = nil;
|
|
72
|
+
resolve(nil);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
- (void)getCurrentPosition:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
76
|
+
{
|
|
77
|
+
CLLocation *last = self.locationManager.location;
|
|
78
|
+
if (last != nil && last.horizontalAccuracy >= 0 &&
|
|
79
|
+
fabs([last.timestamp timeIntervalSinceNow]) < 60) {
|
|
80
|
+
resolve([self dictionaryFromLocation:last]);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
self.pendingResolve = resolve;
|
|
84
|
+
self.pendingReject = reject;
|
|
85
|
+
[self.locationManager requestLocation];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
- (void)isTracking:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
89
|
+
{
|
|
90
|
+
resolve(@(self.tracking));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
#pragma mark - CLLocationManagerDelegate
|
|
94
|
+
|
|
95
|
+
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
|
|
96
|
+
{
|
|
97
|
+
CLLocation *location = locations.lastObject;
|
|
98
|
+
if (location == nil) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
NSDictionary *fix = [self dictionaryFromLocation:location];
|
|
102
|
+
if (self.pendingResolve != nil) {
|
|
103
|
+
self.pendingResolve(fix);
|
|
104
|
+
self.pendingResolve = nil;
|
|
105
|
+
self.pendingReject = nil;
|
|
106
|
+
}
|
|
107
|
+
if (self.tracking) {
|
|
108
|
+
[self emitOnLocation:fix];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
|
|
113
|
+
{
|
|
114
|
+
NSString *message = error.localizedDescription ?: @"Position unavailable";
|
|
115
|
+
if (self.pendingReject != nil) {
|
|
116
|
+
self.pendingReject(@"E_LOCATION", message, error);
|
|
117
|
+
self.pendingResolve = nil;
|
|
118
|
+
self.pendingReject = nil;
|
|
119
|
+
}
|
|
120
|
+
[self emitOnError:@{@"code" : @(2), @"message" : message}];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager
|
|
124
|
+
{
|
|
125
|
+
CLAuthorizationStatus status = manager.authorizationStatus;
|
|
126
|
+
if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
|
|
127
|
+
if (self.pendingReject != nil) {
|
|
128
|
+
self.pendingReject(@"E_PERMISSION", @"Location permission not granted", nil);
|
|
129
|
+
self.pendingResolve = nil;
|
|
130
|
+
self.pendingReject = nil;
|
|
131
|
+
}
|
|
132
|
+
[self emitOnError:@{@"code" : @(1), @"message" : @"Location permission not granted"}];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
137
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
138
|
+
{
|
|
139
|
+
return std::make_shared<facebook::react::NativeReactNativeBackgroundGeolocationSpecJSI>(params);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
+ (NSString *)moduleName
|
|
143
|
+
{
|
|
144
|
+
return @"ReactNativeBackgroundGeolocation";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Codegen spec for the ReactNativeBackgroundGeolocation TurboModule.
|
|
7
|
+
*
|
|
8
|
+
* This file is the contract between TypeScript and the native engines.
|
|
9
|
+
* React Native's codegen generates the Kotlin and Objective-C++ bindings
|
|
10
|
+
* from the types declared here, so every public type must stay
|
|
11
|
+
* codegen-compatible (plain object type aliases, no interfaces for data).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export default TurboModuleRegistry.getEnforcing('ReactNativeBackgroundGeolocation');
|
|
15
|
+
//# sourceMappingURL=NativeReactNativeBackgroundGeolocation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeReactNativeBackgroundGeolocation.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;;AAGpE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AA4DA,eAAeA,mBAAmB,CAACC,YAAY,CAC7C,kCACF,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { haversineMeters } from "../location.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fix acceptance rules. Raw GPS is noisy: fixes arrive with poor accuracy,
|
|
7
|
+
* out of order, or jittering around a point while the device stands still.
|
|
8
|
+
* A fix is only worth recording when it passes every rule below.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_FILTER_OPTIONS = {
|
|
12
|
+
maxAccuracyMeters: 50,
|
|
13
|
+
minDistanceMeters: 5
|
|
14
|
+
};
|
|
15
|
+
/** True when the fix's own reported accuracy is good enough to trust. */
|
|
16
|
+
export function hasAcceptableAccuracy(fix, options) {
|
|
17
|
+
return fix.accuracy >= 0 && fix.accuracy <= options.maxAccuracyMeters;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** True when the fix is newer than the last accepted one (no time travel). */
|
|
21
|
+
export function isChronological(fix, last) {
|
|
22
|
+
return !last || fix.timestamp > last.timestamp;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** True when the fix moved far enough from the last accepted fix to matter. */
|
|
26
|
+
export function isFarEnough(fix, last, options) {
|
|
27
|
+
if (!last) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return haversineMeters(last, fix) >= options.minDistanceMeters;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** All rules combined: should this fix be recorded? */
|
|
34
|
+
export function shouldAcceptFix(fix, last, options = DEFAULT_FILTER_OPTIONS) {
|
|
35
|
+
return hasAcceptableAccuracy(fix, options) && isChronological(fix, last) && isFarEnough(fix, last, options);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["haversineMeters","DEFAULT_FILTER_OPTIONS","maxAccuracyMeters","minDistanceMeters","hasAcceptableAccuracy","fix","options","accuracy","isChronological","last","timestamp","isFarEnough","shouldAcceptFix"],"sourceRoot":"../../../src","sources":["geo/filter.ts"],"mappings":";;AAAA,SAASA,eAAe,QAAyB,gBAAa;;AAE9D;AACA;AACA;AACA;AACA;;AASA,OAAO,MAAMC,sBAAqC,GAAG;EACnDC,iBAAiB,EAAE,EAAE;EACrBC,iBAAiB,EAAE;AACrB,CAAC;AAOD;AACA,OAAO,SAASC,qBAAqBA,CACnCC,GAAY,EACZC,OAAsB,EACb;EACT,OAAOD,GAAG,CAACE,QAAQ,IAAI,CAAC,IAAIF,GAAG,CAACE,QAAQ,IAAID,OAAO,CAACJ,iBAAiB;AACvE;;AAEA;AACA,OAAO,SAASM,eAAeA,CAACH,GAAY,EAAEI,IAAc,EAAW;EACrE,OAAO,CAACA,IAAI,IAAIJ,GAAG,CAACK,SAAS,GAAGD,IAAI,CAACC,SAAS;AAChD;;AAEA;AACA,OAAO,SAASC,WAAWA,CACzBN,GAAY,EACZI,IAAyB,EACzBH,OAAsB,EACb;EACT,IAAI,CAACG,IAAI,EAAE;IACT,OAAO,IAAI;EACb;EACA,OAAOT,eAAe,CAACS,IAAI,EAAEJ,GAAG,CAAC,IAAIC,OAAO,CAACH,iBAAiB;AAChE;;AAEA;AACA,OAAO,SAASS,eAAeA,CAC7BP,GAAY,EACZI,IAAyB,EACzBH,OAAsB,GAAGL,sBAAsB,EACtC;EACT,OACEG,qBAAqB,CAACC,GAAG,EAAEC,OAAO,CAAC,IACnCE,eAAe,CAACH,GAAG,EAAEI,IAAI,CAAC,IAC1BE,WAAW,CAACN,GAAG,EAAEI,IAAI,EAAEH,OAAO,CAAC;AAEnC","ignoreList":[]}
|