Closed thur-c closed 1 month ago
Post your code.
why are you using .watchPosition? It’s generally not required.
I am using watchPosition because I'm developing an app that fetches the user location while it's on the background. It's just a test I'm making. WatchPosition works well with expo or it's better to use in in pure react native? It works well with Android but on IOS it crashes. Is expo the problem or am I missing something here?
`
const [currentLocation, setCurrentLocation] = useState
function configBackgroundGeolocation() {
BackgroundGeolocation.ready({
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 0,
stopTimeout: 1,
debug: false,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
disableMotionActivityUpdates: true,
stopOnTerminate: false,
startOnBoot: true,
locationUpdateInterval: 1000,
fastestLocationUpdateInterval: 1000,
useSignificantChangesOnly: false,
disableElasticity: true,
url: 'http://my.server.com',
params: {
user_id: 123,
},
headers: {
'my-auth-token': 'secret-key',
},
}).then(() => {
setEnabled(true);
console.log('- BackgroundGeolocation is configured and ready: ', enabled);
});
}
useEffect(() => {
try {
configBackgroundGeolocation();
} catch (error) {
console.error(error.message);
} finally {
console.log('Finished processing background geolocation');
}
}, []);
async function logs(){
const Logger = BackgroundGeolocation.logger;
const log = await Logger.getLog();
console.log(log);
}
useEffect(() => {
try {
if (enabled) {
BackgroundGeolocation.start();
// Watch position updates
BackgroundGeolocation.watchPosition(
(location) => {
// console.log('[watchPosition] -', location.coords.speed);
setCurrentLocation(location);
},
(errorCode) => {
console.log('[watchPosition] ERROR -', errorCode);
},
{
interval: 1000,
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
persist: true,
timeout: 60000,
}
);
// Listener for location updates
BackgroundGeolocation.on('location', (location: Location) => {
setCurrentLocation(location);
});
logs();
} else {
BackgroundGeolocation.stop();
}
} catch (error) {
console.log(error);
}
}, [enabled]);
`
.watchPosition should be avoided. It’s designed for use only for short periods of time in the foreground. You probably don’t need it.
With debug: true
, after calling .start(), go outside for a ~1km walk. That’s all you need to do. The plug-in will automatically begin tracking after the device is detected to be moving (requires movement of ~200 meters.)
see wiki here “Philosophy of Operation”.
Thanks a lot! It's work for me, I'm closing now!
App on IOS crashes while using BackgroundGeolocation.watchPosition
Expected Behavior
It shouldn't crash and works when gets current position
Actual Behavior
I notice when I use BackgroundGeolocation.watchPosition the app crashes and closes.
Steps to Reproduce
Context
Debug logs
Xcode Logs
``` Thread 1: "Error when sending event: enabledchange with body: 0. RCTCallableJSModules is not set. This is probably because you've explicitly synthesized the RCTCallableJSModules in RNBackgroundGeolocation, even though it's inherited from RCTEventEmitter." ```Plugin Logs
``` 2024-10-10 10:09:42.452 ╔═══════════════════════════════════════════════════════════ ║ -[TSHttpService finish:error:] Success: 0 ╚═══════════════════════════════════════════════════════════ 2024-10-10 10:09:42.452 ✅-[BackgroundTaskManager stopBackgroundTask:]_block_invoke 13 OF ( 11, 13 ) 2024-10-10 10:09:43.428 🎾-[LocationManager onWatchPositionTimer:] ONESHOT 2024-10-10 10:09:53.430 📍<-23.47537242,-47.42686657> +/- 35.00m (speed -1.00 mps / course -1.00) @ 10/10/24, 10:09:53 Brasilia Standard Time 2024-10-10 10:09:53.430 🔴-[LocationManager stopUpdatingLocation] OFF 2024-10-10 10:09:53.430 ╔═══════════════════════════════════════════════════════════ ║ -[LocationManager locationManager:didUpdateLocations:] Sample 1 of 1 (336 ms old) ╚═══════════════════════════════════════════════════════════ 2024-10-10 10:09:53.431 ℹ️-[PolygonGeofencingService setLocation:] Already updating location