I am trying to log my GPS coordinates in a React-Native expo application.
The background task runs successfully and consoles log the coordinates.
Now I want to send them to my server.
For example a post request to /api/mylocation?lon=123&lat=345
I tried a regular fetch method but I get a timeout error.
regular meaning fetch(url,{ method:’POST’ etc}) (I know how to perform a basic fetch)
How can I solve this? I suspect it has to do with async
and all that.
const LOCATION_TRACKING = 'location-tracking';
const startLocationTracking = async () => {
console.log('startLocationTracking');
await Location.startLocationUpdatesAsync(LOCATION_TRACKING, {
accuracy: Location.Accuracy.Highest,
timeInterval: 10000,
distanceInterval: 0,
});
const hasStarted = await Location.hasStartedLocationUpdatesAsync(
LOCATION_TRACKING
);
console.log('tracking started?', hasStarted);
};
TaskManager.defineTask(LOCATION_TRACKING, async ({ data, error }) => {
console.log('running task:', data, error);
// send_driver_location()
if (error) {
console.log('LOCATION_TRACKING task ERROR:', error);
return;
}
if (data) {
const { locations } = data;
let lat = locations[0].coords.latitude;
let long = locations[0].coords.longitude;
console.log(
`${new Date(Date.now()).toLocaleString()}: ${lat},${long}`
);
// add a fetch method here?
// fetch(url, {...}
}
});
Obviously, this will be helpful when I want to fetch tokens from my server for authentication purposes also.