11# @gabriel-sisjr/react-native-background-location
22
3+ [ ![ NPM Version] ( https://img.shields.io/npm/v/%40gabriel-sisjr%2Freact-native-background-location )] ( https://www.npmjs.com/package/@gabriel-sisjr/react-native-background-location )
4+ [ ![ NPM Beta] ( https://img.shields.io/npm/v/%40gabriel-sisjr%2Freact-native-background-location/beta )] ( https://www.npmjs.com/package/@gabriel-sisjr/react-native-background-location/v/beta )
5+ [ ![ NPM Downloads] ( https://img.shields.io/npm/dm/%40gabriel-sisjr%2Freact-native-background-location )] ( https://www.npmjs.com/package/@gabriel-sisjr/react-native-background-location )
6+ [ ![ NPM Total Downloads] ( https://img.shields.io/npm/dt/%40gabriel-sisjr%2Freact-native-background-location )] ( https://www.npmjs.com/package/@gabriel-sisjr/react-native-background-location )
7+ [ ![ Pre-release CI] ( https://github.com/gabriel-sisjr/react-native-background-location/actions/workflows/prerelease.yml/badge.svg?branch=develop&label=Pre-release )] ( https://github.com/gabriel-sisjr/react-native-background-location/actions/workflows/prerelease.yml )
8+ [ ![ Release CI] ( https://github.com/gabriel-sisjr/react-native-background-location/actions/workflows/publish.yml/badge.svg?branch=main&label=Release )] ( https://github.com/gabriel-sisjr/react-native-background-location/actions/workflows/publish.yml )
9+ [ ![ GitHub Stars] ( https://img.shields.io/github/stars/gabriel-sisjr/react-native-background-location )] ( https://github.com/gabriel-sisjr/react-native-background-location/stargazers )
10+ [ ![ License] ( https://img.shields.io/github/license/gabriel-sisjr/react-native-background-location )] ( https://github.com/gabriel-sisjr/react-native-background-location/blob/develop/LICENSE )
11+ [
12+ ![ Bundlephobia] ( https://img.shields.io/bundlephobia/minzip/%40gabriel-sisjr%2Freact-native-background-location?label=size )
13+ ] ( https://bundlephobia.com/package/@gabriel-sisjr/react-native-background-location )
14+ ![ Platform] ( https://img.shields.io/badge/platform-Android-green )
15+ ![ TypeScript] ( https://img.shields.io/badge/TypeScript-Ready-blue )
16+
317A React Native library for tracking location in the background using TurboModules (New Architecture). Track user location even when the app is minimized or in the background.
418
519<video src =" docs/assets/tracking.mp4 " controls muted playsinline loop width =" 100% " ></video >
620
7-
821## Features
922
1023- ✅ ** Background location tracking** - Continues tracking when app is in background
@@ -32,14 +45,14 @@ yarn add @gabriel-sisjr/react-native-background-location
3245
3346``` xml
3447<manifest xmlns : android =" http://schemas.android.com/apk/res/android" >
35-
48+
3649 <!-- Location permissions -->
3750 <uses-permission android : name =" android.permission.ACCESS_FINE_LOCATION" />
3851 <uses-permission android : name =" android.permission.ACCESS_COARSE_LOCATION" />
39-
52+
4053 <!-- Background location for Android 10+ -->
4154 <uses-permission android : name =" android.permission.ACCESS_BACKGROUND_LOCATION" />
42-
55+
4356 <!-- Foreground service permissions -->
4457 <uses-permission android : name =" android.permission.FOREGROUND_SERVICE" />
4558 <uses-permission android : name =" android.permission.FOREGROUND_SERVICE_LOCATION" />
@@ -173,14 +186,17 @@ See the [Real-Time Updates Guide](docs/getting-started/REAL_TIME_UPDATES.md) for
173186You can also use the module API directly:
174187
175188``` typescript
176- import BackgroundLocation , { type Coords } from ' @gabriel-sisjr/react-native-background-location' ;
189+ import BackgroundLocation , {
190+ type Coords ,
191+ } from ' @gabriel-sisjr/react-native-background-location' ;
177192
178193// Recommended: Let the library generate a unique trip ID
179194const tripId = await BackgroundLocation .startTracking ();
180195
181196// Optional: Resume tracking with an existing trip ID (for crash recovery)
182197// Only use this to resume a previously interrupted tracking session
183- const resumedTripId = await BackgroundLocation .startTracking (' existing-trip-123' );
198+ const resumedTripId =
199+ await BackgroundLocation .startTracking (' existing-trip-123' );
184200
185201// Check if tracking is active
186202const status = await BackgroundLocation .isTracking ();
@@ -189,8 +205,8 @@ console.log(status.tripId); // current trip ID if active
189205
190206// Get all locations for a trip
191207const locations: Coords [] = await BackgroundLocation .getLocations (tripId );
192- locations .forEach (location => {
193- console .log (location .latitude ); // string
208+ locations .forEach (( location ) => {
209+ console .log (location .latitude ); // string
194210 console .log (location .longitude ); // string
195211 console .log (location .timestamp ); // number (Unix timestamp in ms)
196212});
@@ -210,31 +226,32 @@ Starts location tracking in background for a new or existing trip.
210226
211227- ** Parameters:**
212228 - ` tripId ` (optional): Existing trip identifier to resume tracking. If omitted, a new UUID will be generated.
213-
229+
214230 ** ⚠️ Important:** Only provide a ` tripId ` when resuming an interrupted tracking session (e.g., after app crash, battery drain, etc.). For new trips, always omit this parameter to let the library generate a unique UUID. This prevents data overwriting and ensures each trip has a unique identifier.
215231
216232- ** Returns:** Promise resolving to the effective trip ID being used.
217233
218- - ** Behavior:**
234+ - ** Behavior:**
219235 - If tracking is already active, returns the current trip ID (idempotent).
220236 - Starts a foreground service on Android with a persistent notification.
221237 - Requires location permissions to be granted.
222238 - ** New trips:** Generates a unique UUID to prevent collisions.
223239 - ** Resuming trips:** Continues collecting locations to the existing trip data.
224240
225- - ** Best Practice:**
241+ - ** Best Practice:**
242+
226243 ``` typescript
227244 // ✅ Good: Start a new trip
228245 const newTripId = await startTracking ();
229-
246+
230247 // ✅ Good: Resume after interruption
231248 const resumedTripId = await startTracking (previousTripId );
232-
249+
233250 // ❌ Avoid: Don't create new trips with custom IDs
234251 const badTripId = await startTracking (' my-custom-id' );
235252 ```
236253
237- - ** Throws:**
254+ - ** Throws:**
238255 - ` PERMISSION_DENIED ` if location permissions are not granted.
239256 - ` START_TRACKING_ERROR ` if unable to start the service.
240257
@@ -268,15 +285,17 @@ Retrieves all stored location points for a specific trip.
268285 - ` tripId ` : The trip identifier.
269286
270287- ** Returns:** Promise resolving to array of location coordinates:
288+
271289 ``` typescript
272290 {
273- latitude : string ; // Latitude as string
274- longitude : string ; // Longitude as string
275- timestamp : number ; // Unix timestamp in milliseconds
276- }[]
291+ latitude : string ; // Latitude as string
292+ longitude : string ; // Longitude as string
293+ timestamp : number ; // Unix timestamp in milliseconds
294+ }
295+ [];
277296 ```
278297
279- - ** Throws:**
298+ - ** Throws:**
280299 - ` INVALID_TRIP_ID ` if trip ID is empty.
281300 - ` GET_LOCATIONS_ERROR ` if unable to retrieve data.
282301
@@ -333,6 +352,7 @@ On Android, some manufacturers (Xiaomi, Huawei, etc.) have aggressive battery op
333352## Simulator/Emulator Support
334353
335354When the native module is not available (e.g., running in simulator without proper setup), all methods will:
355+
336356- Log a warning to the console
337357- Return safe fallback values
338358- Not crash the app
@@ -349,10 +369,10 @@ Manages location permissions including background permissions.
349369
350370``` typescript
351371const {
352- permissionStatus, // Current permission state
353- requestPermissions, // Request all permissions
354- checkPermissions, // Check without requesting
355- isRequesting, // Loading state
372+ permissionStatus, // Current permission state
373+ requestPermissions, // Request all permissions
374+ checkPermissions, // Check without requesting
375+ isRequesting, // Loading state
356376} = useLocationPermissions ();
357377```
358378
@@ -362,21 +382,21 @@ Complete hook for managing tracking, locations, and state.
362382
363383``` typescript
364384const {
365- isTracking, // Whether tracking is active
366- tripId, // Current trip ID
367- locations, // Array of locations
368- isLoading, // Loading state
369- error, // Last error
370- startTracking, // Start tracking
371- stopTracking, // Stop tracking
372- refreshLocations, // Refresh locations
373- clearCurrentTrip, // Clear trip data
374- clearError, // Clear error
385+ isTracking, // Whether tracking is active
386+ tripId, // Current trip ID
387+ locations, // Array of locations
388+ isLoading, // Loading state
389+ error, // Last error
390+ startTracking, // Start tracking
391+ stopTracking, // Stop tracking
392+ refreshLocations, // Refresh locations
393+ clearCurrentTrip, // Clear trip data
394+ clearError, // Clear error
375395} = useBackgroundLocation ({
376- autoStart: false , // Auto-start on mount
377- onTrackingStart : (id ) => {}, // Callback
378- onTrackingStop : () => {}, // Callback
379- onError : (err ) => {}, // Callback
396+ autoStart: false , // Auto-start on mount
397+ onTrackingStart : (id ) => {}, // Callback
398+ onTrackingStop : () => {}, // Callback
399+ onError : (err ) => {}, // Callback
380400});
381401```
382402
@@ -386,10 +406,10 @@ Lightweight hook for monitoring tracking status.
386406
387407``` typescript
388408const {
389- isTracking, // Whether tracking is active
390- tripId, // Current trip ID
391- refresh, // Refresh status
392- isLoading, // Loading state
409+ isTracking, // Whether tracking is active
410+ tripId, // Current trip ID
411+ refresh, // Refresh status
412+ isLoading, // Loading state
393413} = useLocationTracking (true );
394414```
395415
@@ -418,12 +438,14 @@ See the **[Hooks Guide](docs/getting-started/hooks.md)** for complete documentat
418438## Documentation
419439
420440### Getting Started
441+
421442- ** [ Quick Start Guide] ( docs/getting-started/QUICKSTART.md ) ** - Get up and running in 5 minutes
422443- ** [ Integration Guide] ( docs/getting-started/INTEGRATION_GUIDE.md ) ** - Detailed integration steps for existing apps
423444- ** [ Hooks Guide] ( docs/getting-started/hooks.md ) ** - Complete hooks documentation
424445- ** [ Real-Time Updates Guide] ( docs/getting-started/REAL_TIME_UPDATES.md ) ** - Automatic location watching with useLocationUpdates
425446
426447### Development
448+
427449- ** [ Publishing Guide] ( docs/development/PUBLISHING.md ) ** - How to publish updates to npm
428450- ** [ Implementation Summary] ( docs/development/IMPLEMENTATION_SUMMARY.md ) ** - Technical overview of the implementation
429451- ** [ Testing Guide] ( docs/development/TESTING.md ) ** - Testing structure and guidelines
@@ -464,6 +486,7 @@ yarn ios
464486### TypeScript errors
465487
466488Make sure your ` tsconfig.json ` includes:
489+
467490``` json
468491{
469492 "compilerOptions" : {
0 commit comments