@@ -120,13 +120,15 @@ export default function LeafletMap({ from, to, animateKey, isPredicting }: Leafl
120120 } , [ clearTurnMarkers ] ) ;
121121
122122 const fetchRoute = useCallback ( async ( ) => {
123+ console . log ( `[LeafletMap] Fetching route from ${ from ?. name ?? 'start' } to ${ to ?. name ?? 'end' } .` ) ;
123124 if ( ! from || ! to ) return ;
124125
125126 const apiKey = import . meta. env . VITE_GEOAPIFY_API_KEY ;
126127 if ( ! apiKey ) {
127128 // Fallback to straight line if no API key
128129 if ( mapRef . current ) { // Guard against mapRef.current being null
129130 if ( routeLayerRef . current ) routeLayerRef . current . remove ( ) ;
131+ console . warn ( "[LeafletMap] No API key. Drawing a straight line as a fallback." ) ;
130132 routeLayerRef . current = L . polyline ( [ [ from . lat , from . lon ] , [ to . lat , to . lon ] ] , { color : '#2563eb' , weight : 3 , opacity : 0.85 } ) . addTo ( mapRef . current ) ;
131133 }
132134 return ;
@@ -138,35 +140,46 @@ export default function LeafletMap({ from, to, animateKey, isPredicting }: Leafl
138140 if ( routeLayerRef . current ) routeLayerRef . current . remove ( ) ;
139141
140142 try {
141- const primaryRouteResponse = await fetch (
142- `https://api.geoapify.com/v1/routing?waypoints= ${ from . lat } , ${ from . lon } | ${ to . lat } , ${ to . lon } &mode=drive&format=geojson&waypoints.snapped=true&apiKey= ${ apiKey } `
143- ) ;
143+ const primaryRouteUrl = `https://api.geoapify.com/v1/routing?waypoints= ${ from . lat } , ${ from . lon } | ${ to . lat } , ${ to . lon } &mode=drive&format=geojson&waypoints.snapped=true&apiKey= ${ apiKey } ` ;
144+ console . log ( "[LeafletMap] Primary API Request:" , primaryRouteUrl ) ;
145+ const primaryRouteResponse = await fetch ( primaryRouteUrl ) ;
144146 let routeData = await primaryRouteResponse . json ( ) ;
147+ console . log ( "[LeafletMap] Primary API Response:" , { status : primaryRouteResponse . status , ok : primaryRouteResponse . ok , data : routeData } ) ;
145148
146149 if ( routeData . statusCode === 400 ) {
147- console . warn ( "Initial routing failed. Attempting fallback with reverse geocoding." ) ;
148- const fromPromise = fetch ( `https://api.geoapify.com/v1/geocode/reverse?lat=${ from . lat } &lon=${ from . lon } &apiKey=${ apiKey } ` ) . then ( res => res . json ( ) ) ;
149- const toPromise = fetch ( `https://api.geoapify.com/v1/geocode/reverse?lat=${ to . lat } &lon=${ to . lon } &apiKey=${ apiKey } ` ) . then ( res => res . json ( ) ) ;
150+ console . warn ( "[LeafletMap] Initial routing failed (status 400). Attempting fallback with reverse geocoding." ) ;
151+ const fromRevUrl = `https://api.geoapify.com/v1/geocode/reverse?lat=${ from . lat } &lon=${ from . lon } &apiKey=${ apiKey } ` ;
152+ const toRevUrl = `https://api.geoapify.com/v1/geocode/reverse?lat=${ to . lat } &lon=${ to . lon } &apiKey=${ apiKey } ` ;
153+ console . log ( "[LeafletMap] Fallback Geocode Requests:" , { from : fromRevUrl , to : toRevUrl } ) ;
154+
155+ const fromPromise = fetch ( fromRevUrl ) . then ( res => res . json ( ) ) ;
156+ const toPromise = fetch ( toRevUrl ) . then ( res => res . json ( ) ) ;
150157 const [ fromRev , toRev ] = await Promise . all ( [ fromPromise , toPromise ] ) ;
158+ console . log ( "[LeafletMap] Fallback Geocode Responses:" , { fromRev, toRev } ) ;
151159
152160 const correctedFrom = fromRev ?. features ?. [ 0 ] ?. properties ;
153161 const correctedTo = toRev ?. features ?. [ 0 ] ?. properties ;
154162
155- if ( ! correctedFrom || ! correctedTo ) throw new Error ( "Reverse geocoding failed." ) ;
163+ if ( ! correctedFrom || ! correctedTo ) {
164+ console . error ( "[LeafletMap] Reverse geocoding failed for fallback." , { fromRev, toRev } ) ;
165+ throw new Error ( "Reverse geocoding failed." ) ;
166+ }
156167
157- const fallbackRouteResponse = await fetch (
158- `https://api.geoapify.com/v1/routing?waypoints= ${ correctedFrom . lat } , ${ correctedFrom . lon } | ${ correctedTo . lat } , ${ correctedTo . lon } &mode=drive&format=geojson&apiKey= ${ apiKey } `
159- ) ;
168+ const fallbackRouteUrl = `https://api.geoapify.com/v1/routing?waypoints= ${ correctedFrom . lat } , ${ correctedFrom . lon } | ${ correctedTo . lat } , ${ correctedTo . lon } &mode=drive&format=geojson&apiKey= ${ apiKey } ` ;
169+ console . log ( "[LeafletMap] Fallback API Request:" , fallbackRouteUrl ) ;
170+ const fallbackRouteResponse = await fetch ( fallbackRouteUrl ) ;
160171 if ( ! fallbackRouteResponse . ok ) throw new Error ( `Fallback routing failed.` ) ;
161172 routeData = await fallbackRouteResponse . json ( ) ;
173+ console . log ( "[LeafletMap] Fallback API Response:" , { status : fallbackRouteResponse . status , ok : fallbackRouteResponse . ok , data : routeData } ) ;
162174 }
163175
164- if ( ! routeData ?. features ?. [ 0 ] ) throw new Error ( "No route feature found." ) ;
176+ if ( ! routeData ?. features ?. [ 0 ] ) throw new Error ( "No route feature found in API response ." ) ;
165177
178+ console . log ( "[LeafletMap] Route data received, starting animation." ) ;
166179 animateRoute ( routeData . features [ 0 ] ) ;
167180 } catch ( error ) {
168181 if ( error instanceof Error ) {
169- console . error ( "Final routing error:" , error . message ) ;
182+ console . error ( "[LeafletMap] Final routing error:" , error ) ;
170183 setRouteError ( error . message ) ;
171184 }
172185 } finally {
@@ -180,6 +193,7 @@ export default function LeafletMap({ from, to, animateKey, isPredicting }: Leafl
180193 useEffect ( ( ) => {
181194 if ( mapRef . current ) return ; // Initialize map only once
182195
196+ console . log ( "[LeafletMap] Initializing Leaflet map component." ) ;
183197 mapRef . current = L . map ( 'route-map' , { zoomControl : true } ) ;
184198 const apiKey = import . meta. env . VITE_GEOAPIFY_API_KEY ;
185199 const tileUrl = apiKey
@@ -193,6 +207,7 @@ export default function LeafletMap({ from, to, animateKey, isPredicting }: Leafl
193207
194208 // Cleanup function to remove map on component unmount
195209 return ( ) => {
210+ console . log ( "[LeafletMap] Cleaning up and removing map instance." ) ;
196211 mapRef . current ?. remove ( ) ;
197212 mapRef . current = null ;
198213 } ;
@@ -203,6 +218,8 @@ export default function LeafletMap({ from, to, animateKey, isPredicting }: Leafl
203218 const map = mapRef . current ;
204219 if ( ! map ) return ;
205220
221+ console . log ( "[LeafletMap] Updating start/end markers and map view." ) ;
222+
206223 // Draw start/end markers
207224 markersRef . current . forEach ( m => m . remove ( ) ) ;
208225 markersRef . current = [ ] ;
0 commit comments