chore: fix geo
This commit is contained in:
@@ -19,8 +19,6 @@ import {
|
||||
Modal,
|
||||
StatusBar,
|
||||
ScrollView,
|
||||
Platform,
|
||||
PermissionsAndroid,
|
||||
} from "react-native";
|
||||
import Geolocation from "@react-native-community/geolocation";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
@@ -51,8 +49,6 @@ import TomTomMap, {
|
||||
TomTomMarker,
|
||||
} from "../../components/TomTomMap";
|
||||
|
||||
|
||||
|
||||
const { width: SCREEN_WIDTH } = Dimensions.get("window");
|
||||
const MAP_HEIGHT = 260;
|
||||
const LOCATION_INTERVAL_MS = 15000;
|
||||
@@ -136,38 +132,41 @@ export default function DashboardScreen() {
|
||||
// TomTom Route calculation
|
||||
// Geocode + calcul dans DashboardScreen pour récupérer routeInfo
|
||||
// --------------------------------------------------
|
||||
const calcRoute = useCallback(async (address: string, targetRef?: RefObject<TomTomMapRef | null>) => {
|
||||
const coords = lastCoordsRef.current;
|
||||
if (!coords) {
|
||||
pendingRouteAddress.current = address;
|
||||
return;
|
||||
}
|
||||
pendingRouteAddress.current = address;
|
||||
const ref = targetRef ?? mapRef;
|
||||
if (!ref.current) return;
|
||||
|
||||
setRouteLoading(true);
|
||||
try {
|
||||
const origin = { latitude: coords.lat, longitude: coords.lng };
|
||||
const dest = await geocodeAddress(address);
|
||||
if (!dest) {
|
||||
setRouteLoading(false);
|
||||
const calcRoute = useCallback(
|
||||
async (address: string, targetRef?: RefObject<TomTomMapRef | null>) => {
|
||||
const coords = lastCoordsRef.current;
|
||||
if (!coords) {
|
||||
pendingRouteAddress.current = address;
|
||||
return;
|
||||
}
|
||||
const result = await calculateRoute(origin, dest);
|
||||
if (result) {
|
||||
setRouteInfo(result.route);
|
||||
ref.current?.calcRoute(origin, dest);
|
||||
} else {
|
||||
setRouteInfo(null);
|
||||
ref.current?.calcRoute(origin, dest);
|
||||
pendingRouteAddress.current = address;
|
||||
const ref = targetRef ?? mapRef;
|
||||
if (!ref.current) return;
|
||||
|
||||
setRouteLoading(true);
|
||||
try {
|
||||
const origin = { latitude: coords.lat, longitude: coords.lng };
|
||||
const dest = await geocodeAddress(address);
|
||||
if (!dest) {
|
||||
setRouteLoading(false);
|
||||
return;
|
||||
}
|
||||
const result = await calculateRoute(origin, dest);
|
||||
if (result) {
|
||||
setRouteInfo(result.route);
|
||||
ref.current?.calcRoute(origin, dest);
|
||||
} else {
|
||||
setRouteInfo(null);
|
||||
ref.current?.calcRoute(origin, dest);
|
||||
}
|
||||
} catch {
|
||||
/* silent */
|
||||
} finally {
|
||||
setRouteLoading(false);
|
||||
}
|
||||
} catch {
|
||||
/* silent */
|
||||
} finally {
|
||||
setRouteLoading(false);
|
||||
}
|
||||
}, []);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
// --------------------------------------------------
|
||||
// Data
|
||||
@@ -268,7 +267,7 @@ export default function DashboardScreen() {
|
||||
if (lastCoords && pendingRouteAddress.current) {
|
||||
calcRoute(pendingRouteAddress.current);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [lastCoords]);
|
||||
|
||||
// Quand le fullscreen s'ouvre, rejouer la route sur la carte fullscreen
|
||||
@@ -280,7 +279,7 @@ export default function DashboardScreen() {
|
||||
setTimeout(() => {
|
||||
calcRoute(addr, fullscreenMapRef);
|
||||
}, 1200);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [mapFullscreen]);
|
||||
|
||||
const onRefresh = async () => {
|
||||
@@ -295,19 +294,10 @@ 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"));
|
||||
}
|
||||
}
|
||||
|
||||
(): Promise<Location.LocationObject> =>
|
||||
new Promise((resolve, reject) => {
|
||||
Geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
(position) =>
|
||||
resolve({
|
||||
coords: {
|
||||
latitude: position.coords.latitude,
|
||||
@@ -319,17 +309,13 @@ export default function DashboardScreen() {
|
||||
speed: position.coords.speed ?? 0,
|
||||
},
|
||||
timestamp: position.timestamp,
|
||||
} as Location.LocationObject);
|
||||
},
|
||||
} as Location.LocationObject),
|
||||
(error) => reject(new Error(error.message)),
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 30000,
|
||||
maximumAge: 10000,
|
||||
},
|
||||
// enableHighAccuracy: false → provider réseau/cell (1-3s)
|
||||
// au lieu du GPS pur qui peut prendre 30-60s (cold start)
|
||||
{ enableHighAccuracy: false, timeout: 15000, maximumAge: 10000 },
|
||||
);
|
||||
});
|
||||
},
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
@@ -650,7 +636,10 @@ export default function DashboardScreen() {
|
||||
markers={driverMarkers}
|
||||
initialCenter={
|
||||
lastCoords
|
||||
? { latitude: lastCoords.lat, longitude: lastCoords.lng }
|
||||
? {
|
||||
latitude: lastCoords.lat,
|
||||
longitude: lastCoords.lng,
|
||||
}
|
||||
: { latitude: 48.8566, longitude: 2.3522 }
|
||||
}
|
||||
initialZoom={14}
|
||||
@@ -699,12 +688,24 @@ export default function DashboardScreen() {
|
||||
{routeInfo && !routeLoading && (
|
||||
<View style={styles.routeInfoOverlay}>
|
||||
<View style={styles.routeInfoChip}>
|
||||
<Ionicons name="time-outline" size={13} color={colors.white} />
|
||||
<Text style={styles.routeInfoOverlayText}>{routeInfo.duration}</Text>
|
||||
<Ionicons
|
||||
name="time-outline"
|
||||
size={13}
|
||||
color={colors.white}
|
||||
/>
|
||||
<Text style={styles.routeInfoOverlayText}>
|
||||
{routeInfo.duration}
|
||||
</Text>
|
||||
</View>
|
||||
<View style={styles.routeInfoChip}>
|
||||
<Ionicons name="navigate-outline" size={13} color={colors.white} />
|
||||
<Text style={styles.routeInfoOverlayText}>{routeInfo.distance}</Text>
|
||||
<Ionicons
|
||||
name="navigate-outline"
|
||||
size={13}
|
||||
color={colors.white}
|
||||
/>
|
||||
<Text style={styles.routeInfoOverlayText}>
|
||||
{routeInfo.distance}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
)}
|
||||
@@ -1281,7 +1282,10 @@ export default function DashboardScreen() {
|
||||
markers={driverMarkers}
|
||||
initialCenter={
|
||||
lastCoords
|
||||
? { latitude: lastCoords.lat, longitude: lastCoords.lng }
|
||||
? {
|
||||
latitude: lastCoords.lat,
|
||||
longitude: lastCoords.lng,
|
||||
}
|
||||
: { latitude: 48.8566, longitude: 2.3522 }
|
||||
}
|
||||
initialZoom={15}
|
||||
@@ -1300,7 +1304,9 @@ export default function DashboardScreen() {
|
||||
/>
|
||||
</TouchableOpacity>
|
||||
<Text style={styles.fullscreenTitle}>
|
||||
{pendingRouteAddress.current ? "Itinéraire en cours" : "GPS en direct"}
|
||||
{pendingRouteAddress.current
|
||||
? "Itinéraire en cours"
|
||||
: "GPS en direct"}
|
||||
</Text>
|
||||
<View style={{ width: 40 }} />
|
||||
</View>
|
||||
@@ -1316,7 +1322,10 @@ export default function DashboardScreen() {
|
||||
color={colors.danger}
|
||||
/>
|
||||
<Text
|
||||
style={[styles.fullscreenInfoText, { flex: 1 }]}
|
||||
style={[
|
||||
styles.fullscreenInfoText,
|
||||
{ flex: 1 },
|
||||
]}
|
||||
numberOfLines={2}
|
||||
>
|
||||
{pendingRouteAddress.current}
|
||||
@@ -1328,12 +1337,29 @@ export default function DashboardScreen() {
|
||||
{routeInfo && (
|
||||
<View style={styles.fullscreenInfoRow}>
|
||||
<View style={styles.routeInfoChip}>
|
||||
<Ionicons name="time-outline" size={14} color={colors.white} />
|
||||
<Text style={styles.routeInfoOverlayText}>{routeInfo.duration}</Text>
|
||||
<Ionicons
|
||||
name="time-outline"
|
||||
size={14}
|
||||
color={colors.white}
|
||||
/>
|
||||
<Text style={styles.routeInfoOverlayText}>
|
||||
{routeInfo.duration}
|
||||
</Text>
|
||||
</View>
|
||||
<View style={[styles.routeInfoChip, { marginLeft: spacing.s }]}>
|
||||
<Ionicons name="navigate-outline" size={14} color={colors.white} />
|
||||
<Text style={styles.routeInfoOverlayText}>{routeInfo.distance}</Text>
|
||||
<View
|
||||
style={[
|
||||
styles.routeInfoChip,
|
||||
{ marginLeft: spacing.s },
|
||||
]}
|
||||
>
|
||||
<Ionicons
|
||||
name="navigate-outline"
|
||||
size={14}
|
||||
color={colors.white}
|
||||
/>
|
||||
<Text style={styles.routeInfoOverlayText}>
|
||||
{routeInfo.distance}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
)}
|
||||
@@ -1354,8 +1380,14 @@ export default function DashboardScreen() {
|
||||
GPS {locationEnabled ? "actif" : "inactif"}
|
||||
</Text>
|
||||
{lastCoords && (
|
||||
<Text style={[styles.fullscreenCoords, { marginLeft: 8 }]}>
|
||||
{lastCoords.lat.toFixed(4)}, {lastCoords.lng.toFixed(4)}
|
||||
<Text
|
||||
style={[
|
||||
styles.fullscreenCoords,
|
||||
{ marginLeft: 8 },
|
||||
]}
|
||||
>
|
||||
{lastCoords.lat.toFixed(4)},{" "}
|
||||
{lastCoords.lng.toFixed(4)}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
@@ -1364,10 +1396,19 @@ export default function DashboardScreen() {
|
||||
{pendingRouteAddress.current && lastCoords && (
|
||||
<TouchableOpacity
|
||||
style={styles.calcRouteBtn}
|
||||
onPress={() => calcRoute(pendingRouteAddress.current!, fullscreenMapRef)}
|
||||
onPress={() =>
|
||||
calcRoute(
|
||||
pendingRouteAddress.current!,
|
||||
fullscreenMapRef,
|
||||
)
|
||||
}
|
||||
activeOpacity={0.7}
|
||||
>
|
||||
<Ionicons name="navigate" size={16} color={colors.white} />
|
||||
<Ionicons
|
||||
name="navigate"
|
||||
size={16}
|
||||
color={colors.white}
|
||||
/>
|
||||
<Text style={styles.calcRouteBtnText}>
|
||||
Recalculer l'itinéraire
|
||||
</Text>
|
||||
|
||||
Reference in New Issue
Block a user