react-native geolocation api with background support
npm install react-native-mo-geolocationGeolocation and Background-Geolocation API
Install just like your ordinary react-native module.
Please check the example/ code.
``ts
import { Geolocation, GeolocationAccuracy, GeolocationPermissionStatus } from 'react-native-mo-geolocation';
if (await Geolocation.requestPermissions({ background: true }) === GeolocationPermissionStatus.DENIED) {
Geolocation.showSettings();
}
const location = await Geolocation.get({ accuracy: GeolocationAccuracy.BEST });
const sub = Geolocation.observe({
background: true,
indicateBackground: true,
accuracy: GeolocationAccuracy.BEST,
}).subscribe((pos) => {
if (pos instanceof Error) {
console.log('we had an error!');
} else {
console.log('new position', pos);
}
});
// ...
sub.remove();
``
- For iOS you need to pass indicateBackground: true to prevent the app from
getting terminated.