chore: fix geoloca
This commit is contained in:
+5
@@ -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": {
|
||||
"version": "2.28.0",
|
||||
"resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz",
|
||||
|
||||
Generated
+6
@@ -24,6 +24,7 @@
|
||||
"react": "19.1.0",
|
||||
"react-dom": "19.1.0",
|
||||
"react-native": "0.81.5",
|
||||
"react-native-geolocation-service": "^5.3.1",
|
||||
"react-native-gesture-handler": "~2.28.0",
|
||||
"react-native-maps": "1.20.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": {
|
||||
"version": "2.28.0",
|
||||
"resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
"react": "19.1.0",
|
||||
"react-dom": "19.1.0",
|
||||
"react-native": "0.81.5",
|
||||
"react-native-geolocation-service": "^5.3.1",
|
||||
"react-native-gesture-handler": "~2.28.0",
|
||||
"react-native-maps": "1.20.1",
|
||||
"react-native-reanimated": "~4.1.1",
|
||||
|
||||
@@ -19,7 +19,10 @@ import {
|
||||
Modal,
|
||||
StatusBar,
|
||||
ScrollView,
|
||||
Platform,
|
||||
PermissionsAndroid,
|
||||
} from "react-native";
|
||||
import Geolocation from "react-native-geolocation-service";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import * as Location from "expo-location";
|
||||
import { spacing, fontSize, borderRadius } from "../../theme";
|
||||
@@ -291,29 +294,58 @@ export default function DashboardScreen() {
|
||||
// --------------------------------------------------
|
||||
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 () => {
|
||||
if (sendingRef.current) return;
|
||||
sendingRef.current = true;
|
||||
try {
|
||||
const loc = await Promise.race([
|
||||
Location.getCurrentPositionAsync({
|
||||
accuracy: Location.Accuracy.High,
|
||||
}),
|
||||
new Promise<never>((_, reject) =>
|
||||
setTimeout(() => reject(new Error("GPS timeout")), 8000),
|
||||
),
|
||||
]);
|
||||
const loc = await getLocationWithFallback();
|
||||
|
||||
if (
|
||||
!loc ||
|
||||
typeof loc !== "object" ||
|
||||
!("coords" in loc) ||
|
||||
!loc.coords
|
||||
) {
|
||||
if (!loc?.coords) {
|
||||
return;
|
||||
}
|
||||
|
||||
const coords = (loc as Location.LocationObject).coords;
|
||||
const coords = loc.coords;
|
||||
const { latitude, longitude } = coords;
|
||||
|
||||
const wasNull = !lastCoordsRef.current;
|
||||
@@ -333,7 +365,7 @@ export default function DashboardScreen() {
|
||||
} finally {
|
||||
sendingRef.current = false;
|
||||
}
|
||||
}, []);
|
||||
}, [getLocationWithFallback]);
|
||||
|
||||
const startLocationTracking = useCallback(async () => {
|
||||
const { status: fgStatus } =
|
||||
|
||||
Reference in New Issue
Block a user