import React, { useState, useEffect, useRef } from 'react'; import { View, Text } from 'react-native'; import MapView, { Marker, Polyline, PROVIDER_DEFAULT } from 'react-native-maps'; import { useAppTheme } from '@theme'; import { Coordinates } from '@app-types/index'; import { getStyles } from './mapView.styles'; import { getRouteDirections, getHaversineDistance, } from '@services/locationService'; interface MapViewComponentProps { origin?: Coordinates; destination?: Coordinates; currentLocation?: Coordinates; showRoute?: boolean; style?: any; } export const MapViewComponent: React.FC = ({ origin, destination, currentLocation, showRoute = false, style, }) => { const { colors } = useAppTheme(); const styles = getStyles(colors); const mapRef = useRef(null); const [routeCoords, setRouteCoords] = useState([]); const lastFetchedOrigin = useRef(null); const defaultRegion = { latitude: currentLocation?.latitude || origin?.latitude || 12.9716, longitude: currentLocation?.longitude || origin?.longitude || 77.5946, latitudeDelta: 0.05, longitudeDelta: 0.05, }; // Fetch routing coordinates useEffect(() => { let active = true; if (showRoute && origin && destination) { // Calculate distance from last fetched origin if (lastFetchedOrigin.current) { const dist = getHaversineDistance(origin, lastFetchedOrigin.current); // If driver moved less than 50 meters (0.05 km), skip re-fetching to prevent spamming if (dist < 0.05 && routeCoords.length > 0) { return; } } getRouteDirections(origin, destination) .then(points => { if (active) { setRouteCoords(points); lastFetchedOrigin.current = origin; } }) .catch(err => { console.error('Error in MapViewComponent getRouteDirections:', err); if (active) { setRouteCoords([origin, destination]); } }); } else { setRouteCoords([]); lastFetchedOrigin.current = null; } return () => { active = false; }; }, [ showRoute, origin?.latitude, origin?.longitude, destination?.latitude, destination?.longitude, routeCoords.length, ]); // Adjust camera to fit all coordinates useEffect(() => { const coordsToFit: Coordinates[] = []; if (currentLocation) { coordsToFit.push(currentLocation); } if (origin) { coordsToFit.push(origin); } if (destination) { coordsToFit.push(destination); } if (coordsToFit.length > 0 && mapRef.current) { const timer = setTimeout(() => { mapRef.current?.fitToCoordinates(coordsToFit, { edgePadding: { top: 60, right: 60, bottom: 60, left: 60 }, animated: true, }); }, 500); return () => clearTimeout(timer); } }, [ currentLocation?.latitude, currentLocation?.longitude, origin?.latitude, origin?.longitude, destination?.latitude, destination?.longitude, ]); return ( {currentLocation && ( )} {origin && ( )} {destination && ( )} {showRoute && routeCoords.length > 0 && ( )} 📍 Live Navigation Map Active ); }; export default MapViewComponent;