diff --git a/apps/edr-freight-web/backoffice/src/pages/fleet/TrackingPage.tsx b/apps/edr-freight-web/backoffice/src/pages/fleet/TrackingPage.tsx index 658af8f2b..14bb32d33 100644 --- a/apps/edr-freight-web/backoffice/src/pages/fleet/TrackingPage.tsx +++ b/apps/edr-freight-web/backoffice/src/pages/fleet/TrackingPage.tsx @@ -60,6 +60,15 @@ const StatBox = ({ label, value }: { label: string; value: string }) => ( type LatLng = { lat: number; lng: number }; +/** Flip a flag once the map (and thus the Maps JS classes) is loaded. */ +function ReadyProbe({ onReady }: { onReady: () => void }) { + const map = useMap(); + useEffect(() => { + if (map) onReady(); + }, [map, onReady]); + return null; +} + /** Fit the map to the current markers (or center on a single one). */ function FitBounds({ points }: { points: LatLng[] }) { const map = useMap(); @@ -81,7 +90,7 @@ function FitBounds({ points }: { points: LatLng[] }) { function useAddress(lat: number, lng: number): string | null { const [addr, setAddr] = useState(null); useEffect(() => { - if (typeof google === "undefined") return; + if (typeof google === "undefined" || !google.maps?.Geocoder) return; setAddr(null); let cancelled = false; new google.maps.Geocoder().geocode({ location: { lat, lng } }, (res, status) => { @@ -144,6 +153,7 @@ export function TrackingPage() { const qc = useQueryClient(); const [selectedId, setSelectedId] = useState(null); const [hoverId, setHoverId] = useState(null); + const [mapsReady, setMapsReady] = useState(false); const [modalOpen, setModalOpen] = useState(false); const [editDevice, setEditDevice] = useState(null); const [form, setForm] = useState({ imei: "", name: "", vehicleId: "" }); @@ -203,7 +213,8 @@ export function TrackingPage() { // Teardrop pin colored by state with a white truck glyph inside. const markerIcon = (d: GpsDevice, selectedFlag: boolean): google.maps.Icon | undefined => { - if (typeof google === "undefined") return undefined; + // Maps API loads async — Size/Point classes may not exist yet at first render. + if (typeof google === "undefined" || !google.maps?.Size || !mapsReady) return undefined; const color = selectedFlag ? freightBrand.primary : d.online ? "#2f80ed" : "#95a5a6"; const svg = ` @@ -304,6 +315,7 @@ export function TrackingPage() { disableDefaultUI={false} style={{ width: "100%", height: "100%" }} > + setMapsReady(true)} /> {positioned.map(({ d, lat, lng }) => (