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 { StyleSheet, Text, View } from 'react-native';
|
||||||
import { NavigationContainer } from '@react-navigation/native';
|
import { NavigationContainer } from '@react-navigation/native';
|
||||||
import { createStackNavigator } from '@react-navigation/stack';
|
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 ListScreen from './src/screens/ListScreen';
|
||||||
import * as Location from "expo-location"
|
import * as Location from "expo-location";
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
|
import Storage from './src/system/database/storage';
|
||||||
|
|
||||||
const Stack = createStackNavigator();
|
const Stack = createStackNavigator();
|
||||||
|
const storage = Storage.getInstance(); // Criando apenas uma instância de Storage
|
||||||
|
|
||||||
let foregroundSubscription = null;
|
|
||||||
|
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
|
const locationInterval = useRef(null); // UseRef para armazenar o intervalo
|
||||||
|
|
||||||
const foregroundUpdate = async() =>{
|
useEffect(() => {
|
||||||
const {granted} = await Location.getForegroundPermissionsAsync()
|
const getLocation = async () => {
|
||||||
if (!granted){
|
const { status } = await Location.requestForegroundPermissionsAsync();
|
||||||
console.error("Permissão Não obtida para localização")
|
if (status !== "granted") {
|
||||||
return
|
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 (
|
return (
|
||||||
<NavigationContainer>
|
<NavigationContainer>
|
||||||
<Stack.Navigator>
|
<Stack.Navigator>
|
||||||
|
|
@ -41,12 +63,3 @@ export default function App() {
|
||||||
</NavigationContainer>
|
</NavigationContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
|
||||||
container: {
|
|
||||||
flex: 1,
|
|
||||||
backgroundColor: '#fff',
|
|
||||||
alignItems: 'center',
|
|
||||||
justifyContent: 'center',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue