React native library for providing low-power, background geographic location changes.
Not supported on Android yet.
On iOS it uses the significant-change location service . It can supply location updates in the background without having to use the Location Updates background mode [view apple docs].
Add the dependency (its not on npm yet)
npm install react-native-location-change@https://github.com/npomfret/react-native-location-change.git --save
react-native link react-native-location-change
Standard RN installation instructions for a native module are below, but you also need to modify your info.plist
file:
iOS 11 and higher: NSLocationAlwaysAndWhenInUseUsageDescription This is needed to get location updates NSLocationAlwaysUsageDescription This is needed to get location updates in the background
Prior to iOS 11: NSLocationAlwaysAndWhenInUseUsageDescription This is needed to get location updates NSLocationAlwaysUsageDescription This is needed to get location updates in the background
If you'd like background updates, you may also need to add the following UI background modes to youy info.plist
file:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
</array>
import {AppRegistry, StyleSheet, Text, View, NativeModules, NativeEventEmitter} from "react-native";
const _EVENT_EMITTER = new NativeEventEmitter(NativeModules.RNLocationChange);
export default class LocationChangeExample extends Component {
componentWillMount() {
this.setState({location: null});
_EVENT_EMITTER.addListener('significantLocationChange', (data) => {
console.log("event:", data);
this.setState({location: data});
});
NativeModules.RNLocationChange.start();
}
componentWillUnmount() {
NativeModules.RNLocationChange.stop();
}
An working example is here.