Modificado o App.js para obter a localização a cada 30s usando o useEffect()
This commit is contained in:
parent
296727ead2
commit
4869e6c47e
|
|
@ -2,36 +2,58 @@ import { StatusBar } from 'expo-status-bar';
|
|||
import { StyleSheet, Text, View } from 'react-native';
|
||||
import { NavigationContainer } from '@react-navigation/native';
|
||||
import { createStackNavigator } from '@react-navigation/stack';
|
||||
import MapScreen from './src/screens/MapScreen'
|
||||
import MapScreen from './src/screens/MapScreen';
|
||||
import ListScreen from './src/screens/ListScreen';
|
||||
import * as Location from "expo-location"
|
||||
import { useEffect } from 'react';
|
||||
import * as Location from "expo-location";
|
||||
import { useEffect, useRef } from 'react';
|
||||
import Storage from './src/system/database/storage';
|
||||
|
||||
const Stack = createStackNavigator();
|
||||
|
||||
|
||||
let foregroundSubscription = null;
|
||||
|
||||
const storage = Storage.getInstance(); // Criando apenas uma instância de Storage
|
||||
|
||||
export default function App() {
|
||||
const locationInterval = useRef(null); // UseRef para armazenar o intervalo
|
||||
|
||||
const foregroundUpdate = async() =>{
|
||||
const {granted} = await Location.getForegroundPermissionsAsync()
|
||||
if (!granted){
|
||||
console.error("Permissão Não obtida para localização")
|
||||
return
|
||||
useEffect(() => {
|
||||
const getLocation = async () => {
|
||||
const { status } = await Location.requestForegroundPermissionsAsync();
|
||||
if (status !== "granted") {
|
||||
console.error("Permissão negada para localização.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
useEffect(()=>{
|
||||
const locationPerm = async() =>{
|
||||
const foregroun = await Location.requestForegroundPermissionsAsync()
|
||||
}
|
||||
locationPerm()
|
||||
foregroundUpdate()
|
||||
|
||||
},[]
|
||||
// Obtém a localização inicial
|
||||
const location = await Location.getCurrentPositionAsync({});
|
||||
global.userLocation = location.coords;
|
||||
await storage.saveData(new Date().valueOf(), location.coords);
|
||||
|
||||
console.log("Localização inicial salva:", location.coords);
|
||||
|
||||
|
||||
// Atualiza a localização a cada 5 segundos
|
||||
locationInterval.current = setInterval(async () => {
|
||||
try {
|
||||
const updatedLocation = await Location.getCurrentPositionAsync({});
|
||||
global.userLocation = updatedLocation.coords;
|
||||
await storage.saveData(new Date().valueOf(), updatedLocation.coords);
|
||||
console.log("Nova localização salva:", updatedLocation.coords);
|
||||
} catch (error) {
|
||||
console.error("Erro ao obter localização:", error);
|
||||
|
||||
}
|
||||
}, 30000);
|
||||
};
|
||||
|
||||
getLocation();
|
||||
|
||||
return () => {
|
||||
if (locationInterval.current) {
|
||||
clearInterval(locationInterval.current);
|
||||
console.log("Intervalo de localização parado.");
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
)
|
||||
return (
|
||||
<NavigationContainer>
|
||||
<Stack.Navigator>
|
||||
|
|
@ -41,12 +63,3 @@ export default function App() {
|
|||
</NavigationContainer>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
backgroundColor: '#fff',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue