Skip to content

gulsher7/react-native-bg-location

Repository files navigation

You can utilize this npm package on iOS right now, and we're in the process of bringing it to Android.

You can utilize this npm package on iOS right now, and we're in the process of bringing it to Android. The react-native-bg-location package is a versatile and easy-to-use library for handling background location tracking in React Native applications. It provides a seamless way to access the device's location in the background, making it suitable for various use cases, including tracking user movements, geofencing.

Table of Contents

  1. Installation
  2. Required permission
  3. Video demo
  4. Usage API
  5. License

1. Installation for ios

npm install react-native-bg-location

```bash

cd ios && pod install

2. Required permission

Open your App in XCode and select the root of your project. Select Capabilities tab. Enable Background Modes and enable the following modes:

  • Location updates
  • Background fetch
Screenshot 2023-10-09 at 10 32 14 PM

Configure Usage Strings in Info.plist

Edit Info.plist. Add the following items (Set Value as desired):

Key Type Value
Privacy - Location Always and When in Use Usage Description String CHANGEME: Location required in background
Privacy - Location When in Use Usage Description String CHANGEME: Location required when app is in use
Privacy - Motion Usage Description String CHANGEME: Motion permission helps detect when device in in-motion

3. Demo video

output.mp4

4. Usage Api

Import the necessary functions and components from the library:

import { LocationEvent, setDesiredDistance, startLocation, stopLocation } from 'react-native-bg-location';

setDesiredDistance

Set the desired distance for location updates. This function takes a numeric parameter distance (in meters) and returns a Promise. For example, if you pass 50, the new latitude and longitude will be received after the device has moved 50 meters.

Example:

setDesiredDistance(50).then((res) => {
  console.log(res);
}).catch((error) => {
  console.log("Error code:", error);
});

startLocation

Call startLocation function to start location tracking.

Example:

startLocation()

stopLocation

Call stopLocation function to stop tracking.

Example:

stopLocation()

LocationEvent.addListener(eventType: string, callback: (data: any) => void)

Add a listener for a specific get lat lng. The callback function will be called whenever the location changed.

Example: 
```jsx
LocationEvent.addListener("LAT_LNG", (latlng) => {
  console.log("Location data:", latlng);
});

remove listner

remove listner to stop getting the lat lng.

Example:

LocationEvent.removeAllListeners("LAT_LNG");

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published