I’m currently trying to test expo location package to locate an iphone both on foreground and background. To do that, I first copied this medium example to have something to try to understand how it works. Everything seems to work fine up until I start the startLocationUpdatesAsync. But then, the function I gave to the TaskManager doesnt seem to be ever executed, be it in the foreground or the background.
If anyone could help me or explain me what is wrong here, that would be really helpfull!
My code :
App.js
import React from 'react';
import { StyleSheet, View } from 'react-native';
import UserLocation from './UserLocation';
export default function App() {
return (
<View style={styles.container}>
<UserLocation />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
UserLocation.js :
import React from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
const LOCATION_TRACKING = 'location-tracking';
var l1;
var l2;
function UserLocation() {
const [locationStarted, setLocationStarted] = React.useState(false);
const startLocationTracking = async () => {
await Location.startLocationUpdatesAsync(LOCATION_TRACKING, {
accuracy: Location.Accuracy.Highest,
timeInterval: 5000,
distanceInterval: 0,
});
const hasStarted = await Location.hasStartedLocationUpdatesAsync(
LOCATION_TRACKING
);
setLocationStarted(hasStarted);
console.log('tracking started?', hasStarted);
};
React.useEffect(() => {
const config = async () => {
let resf = await Location.requestForegroundPermissionsAsync();
let resb = await Location.requestBackgroundPermissionsAsync();
if (resf.status !== 'granted' && resb.status !== 'granted') {
console.log('Permission to access location was denied');
} else {
console.log('Permission to access location granted');
}
};
config();
}, []);
const startLocation = () => {
startLocationTracking();
}
const stopLocation = () => {
setLocationStarted(false);
TaskManager.isTaskRegisteredAsync(LOCATION_TRACKING)
.then((tracking) => {
if (tracking) {
Location.stopLocationUpdatesAsync(LOCATION_TRACKING);
}
})
}
return (
<View>
{locationStarted ?
<TouchableOpacity onPress={stopLocation}>
<Text style={styles.btnText}>Stop Tracking</Text>
</TouchableOpacity>
:
<TouchableOpacity onPress={startLocation}>
<Text style={styles.btnText}>Start Tracking</Text>
</TouchableOpacity>
}
</View>
);
}
const styles = StyleSheet.create({
btnText: {
fontSize: 20,
backgroundColor: 'green',
color: 'white',
paddingHorizontal: 30,
paddingVertical: 10,
borderRadius: 5,
marginTop: 10,
},
});
TaskManager.defineTask(LOCATION_TRACKING, async ({ data, error }) => {
console.log("Testing")
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;
l1 = lat;
l2 = long;
console.log(
`${new Date(Date.now()).toLocaleString()}: ${lat},${long}`
);
}
});
export default UserLocation;
Expected :
LOG Permission to access location granted
LOG tracking started? true
LOG Testing
LOG lat and long
Actual result :
LOG Permission to access location granted
LOG tracking started? true
Masterzeus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.