chore: fix geoloca

This commit is contained in:
2026-02-28 23:18:13 +01:00
parent 9e8c266533
commit 3ce0360d91
4 changed files with 60 additions and 16 deletions
+5
View File
@@ -6614,6 +6614,11 @@
} }
} }
}, },
"node_modules/react-native-geolocation-service": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/react-native-geolocation-service/-/react-native-geolocation-service-5.3.1.tgz",
"integrity": "sha512-LTXPtPNmrdhx+yeWG47sAaCgQc3nG1z+HLLHlhK/5YfOgfLcAb9HAkhREPjQKPZOUx8pKZMIpdGFUGfJYtimXQ=="
},
"node_modules/react-native-gesture-handler": { "node_modules/react-native-gesture-handler": {
"version": "2.28.0", "version": "2.28.0",
"resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz",
+6
View File
@@ -24,6 +24,7 @@
"react": "19.1.0", "react": "19.1.0",
"react-dom": "19.1.0", "react-dom": "19.1.0",
"react-native": "0.81.5", "react-native": "0.81.5",
"react-native-geolocation-service": "^5.3.1",
"react-native-gesture-handler": "~2.28.0", "react-native-gesture-handler": "~2.28.0",
"react-native-maps": "1.20.1", "react-native-maps": "1.20.1",
"react-native-reanimated": "~4.1.1", "react-native-reanimated": "~4.1.1",
@@ -6963,6 +6964,11 @@
} }
} }
}, },
"node_modules/react-native-geolocation-service": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/react-native-geolocation-service/-/react-native-geolocation-service-5.3.1.tgz",
"integrity": "sha512-LTXPtPNmrdhx+yeWG47sAaCgQc3nG1z+HLLHlhK/5YfOgfLcAb9HAkhREPjQKPZOUx8pKZMIpdGFUGfJYtimXQ=="
},
"node_modules/react-native-gesture-handler": { "node_modules/react-native-gesture-handler": {
"version": "2.28.0", "version": "2.28.0",
"resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz",
+1
View File
@@ -25,6 +25,7 @@
"react": "19.1.0", "react": "19.1.0",
"react-dom": "19.1.0", "react-dom": "19.1.0",
"react-native": "0.81.5", "react-native": "0.81.5",
"react-native-geolocation-service": "^5.3.1",
"react-native-gesture-handler": "~2.28.0", "react-native-gesture-handler": "~2.28.0",
"react-native-maps": "1.20.1", "react-native-maps": "1.20.1",
"react-native-reanimated": "~4.1.1", "react-native-reanimated": "~4.1.1",
@@ -19,7 +19,10 @@ import {
Modal, Modal,
StatusBar, StatusBar,
ScrollView, ScrollView,
Platform,
PermissionsAndroid,
} from "react-native"; } from "react-native";
import Geolocation from "react-native-geolocation-service";
import { Ionicons } from "@expo/vector-icons"; import { Ionicons } from "@expo/vector-icons";
import * as Location from "expo-location"; import * as Location from "expo-location";
import { spacing, fontSize, borderRadius } from "../../theme"; import { spacing, fontSize, borderRadius } from "../../theme";
@@ -291,29 +294,58 @@ export default function DashboardScreen() {
// -------------------------------------------------- // --------------------------------------------------
const sendingRef = useRef(false); const sendingRef = useRef(false);
const getLocationWithFallback = useCallback(
(): Promise<Location.LocationObject> => {
return new Promise(async (resolve, reject) => {
if (Platform.OS === "android") {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
return reject(new Error("Permission refusée"));
}
}
Geolocation.getCurrentPosition(
(position) => {
resolve({
coords: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
altitude: position.coords.altitude ?? 0,
accuracy: position.coords.accuracy ?? 0,
altitudeAccuracy: position.coords.altitudeAccuracy ?? 0,
heading: position.coords.heading ?? 0,
speed: position.coords.speed ?? 0,
},
timestamp: position.timestamp,
} as Location.LocationObject);
},
(error) => reject(new Error(error.message)),
{
enableHighAccuracy: true,
timeout: 30000,
maximumAge: 10000,
forceRequestLocation: true,
forceLocationManager: true,
},
);
});
},
[],
);
const sendCurrentLocation = useCallback(async () => { const sendCurrentLocation = useCallback(async () => {
if (sendingRef.current) return; if (sendingRef.current) return;
sendingRef.current = true; sendingRef.current = true;
try { try {
const loc = await Promise.race([ const loc = await getLocationWithFallback();
Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
}),
new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error("GPS timeout")), 8000),
),
]);
if ( if (!loc?.coords) {
!loc ||
typeof loc !== "object" ||
!("coords" in loc) ||
!loc.coords
) {
return; return;
} }
const coords = (loc as Location.LocationObject).coords; const coords = loc.coords;
const { latitude, longitude } = coords; const { latitude, longitude } = coords;
const wasNull = !lastCoordsRef.current; const wasNull = !lastCoordsRef.current;
@@ -333,7 +365,7 @@ export default function DashboardScreen() {
} finally { } finally {
sendingRef.current = false; sendingRef.current = false;
} }
}, []); }, [getLocationWithFallback]);
const startLocationTracking = useCallback(async () => { const startLocationTracking = useCallback(async () => {
const { status: fgStatus } = const { status: fgStatus } =