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.
npm install react-native-bg-location
```bash
cd ios && pod install
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
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 |
output.mp4
Import the necessary functions and components from the library:
import { LocationEvent, setDesiredDistance, startLocation, stopLocation } from 'react-native-bg-location';
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);
});
Call startLocation function to start location tracking.
Example:
startLocation()
Call stopLocation function to stop tracking.
Example:
stopLocation()
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 to stop getting the lat lng.
Example:
LocationEvent.removeAllListeners("LAT_LNG");
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library