@@ -14,16 +14,17 @@ import {
14
14
ChevronRightIcon ,
15
15
ClockIcon ,
16
16
ServerIcon ,
17
- ArrowPathIcon
17
+ ArrowPathIcon ,
18
+ ExclamationCircleIcon
18
19
} from "@heroicons/react/24/outline"
19
20
import Image from "next/image"
20
21
21
22
interface ResultItem {
22
23
api : string
23
24
data : any
24
25
time : string
25
- lat : number
26
- lng : number
26
+ lat : number | null
27
+ lng : number | null
27
28
}
28
29
29
30
import LogoRect from "@/assets/rectangle.svg"
@@ -76,15 +77,22 @@ export default function Home() {
76
77
return ( ) => m . remove ( )
77
78
} , [ ] )
78
79
79
- function goToLocation ( lat : number , lng : number ) {
80
- if ( map ) {
81
- map . flyTo ( { center : [ lng , lat ] , zoom : 9 , speed : 1.2 , curve : 1.4 } )
82
- }
80
+ function goToLocation ( lat : number | null , lng : number | null ) {
81
+ if ( lat === null || lng === null || ! map ) return ;
82
+
83
+ map . flyTo ( {
84
+ center : [ lng , lat ] ,
85
+ zoom : 8 ,
86
+ speed : 1.5 ,
87
+ curve : 1.5
88
+ } )
83
89
}
84
90
85
- function toggleCard ( index : number , lat : number , lng : number ) {
86
- goToLocation ( lat , lng )
91
+ function toggleCard ( index : number , lat : number | null , lng : number | null ) {
87
92
setExpandedCard ( expandedCard === index ? null : index )
93
+ if ( lat !== null && lng !== null ) {
94
+ goToLocation ( lat , lng )
95
+ }
88
96
}
89
97
90
98
function getLocationInfo ( data : any ) {
@@ -138,19 +146,26 @@ export default function Home() {
138
146
data ?. location ?. lat ||
139
147
data ?. location ?. latitude ||
140
148
data ?. data ?. location ?. latitude ||
141
- ( data . loc ? data . loc . split ( "," ) [ 0 ] : 0 )
149
+ ( data . loc ? data . loc . split ( "," ) [ 0 ] : null )
142
150
const lng =
143
151
data . longitude ||
144
152
data . lon ||
145
153
data ?. location ?. lng ||
146
154
data ?. location ?. longitude ||
147
155
data ?. data ?. location ?. longitude ||
148
- ( data . loc ? data . loc . split ( "," ) [ 1 ] : 0 )
149
- if ( lat && lng ) {
156
+ ( data . loc ? data . loc . split ( "," ) [ 1 ] : null )
157
+
158
+ // Ensure both lat and lng are valid numbers before using them
159
+ const validLat = lat !== null && lat !== undefined && ! isNaN ( Number ( lat ) )
160
+ const validLng = lng !== null && lng !== undefined && ! isNaN ( Number ( lng ) )
161
+
162
+ if ( validLat && validLng ) {
163
+ const numLat = Number ( lat )
164
+ const numLng = Number ( lng )
150
165
foundResults = true
151
166
if ( firstCenter ) {
152
167
map . flyTo ( {
153
- center : [ + lng , + lat ] ,
168
+ center : [ numLng , numLat ] ,
154
169
zoom : 8 ,
155
170
speed : 1.5 ,
156
171
curve : 1.5
@@ -171,7 +186,7 @@ export default function Home() {
171
186
</div>
172
187
`
173
188
new maplibregl . Marker ( { element : el } )
174
- . setLngLat ( [ + lng , + lat ] )
189
+ . setLngLat ( [ numLng , numLat ] )
175
190
. setPopup (
176
191
new maplibregl . Popup ( { offset : 25 , className : "custom-popup" } ) . setHTML ( `
177
192
<div class="text-slate-800">
@@ -187,8 +202,21 @@ export default function Home() {
187
202
api,
188
203
data,
189
204
time : ( tEnd - tStart ) . toFixed ( 0 ) ,
190
- lat : + lat ,
191
- lng : + lng
205
+ lat : numLat ,
206
+ lng : numLng
207
+ }
208
+ ] )
209
+ } else {
210
+ console . log ( `No valid coordinates found for ${ api } :` , { lat, lng } )
211
+ // Add the result to the list but mark it as invalid location
212
+ setResults ( ( prev ) => [
213
+ ...prev ,
214
+ {
215
+ api,
216
+ data,
217
+ time : ( tEnd - tStart ) . toFixed ( 0 ) ,
218
+ lat : null ,
219
+ lng : null
192
220
}
193
221
] )
194
222
}
@@ -420,8 +448,9 @@ export default function Home() {
420
448
</ span >
421
449
</ div >
422
450
) : (
423
- < div className = "text-slate-400 text-xs" >
424
- Location data available
451
+ < div className = "flex items-center text-red-500 text-xs" >
452
+ < ExclamationCircleIcon className = "w-3.5 h-3.5 mr-1 flex-shrink-0" />
453
+ < span > No location data available</ span >
425
454
</ div >
426
455
) }
427
456
</ div >
@@ -441,8 +470,13 @@ export default function Home() {
441
470
e . stopPropagation ( )
442
471
goToLocation ( r . lat , r . lng )
443
472
} }
444
- className = "mr-3 flex items-center justify-center w-9 h-9 rounded-lg bg-slate-50 hover:bg-primary-50 border border-slate-200 hover:border-primary-200 transition-colors shadow-sm"
445
- title = "Center on map"
473
+ disabled = { ! r . lat || ! r . lng }
474
+ className = { `mr-3 flex items-center justify-center w-9 h-9 rounded-lg ${
475
+ r . lat && r . lng
476
+ ? "bg-slate-50 hover:bg-primary-50 border border-slate-200 hover:border-primary-200"
477
+ : "bg-slate-50 border border-slate-200 opacity-50 cursor-not-allowed"
478
+ } transition-colors shadow-sm`}
479
+ title = { r . lat && r . lng ? "Center on map" : "No location data" }
446
480
>
447
481
< MapPinIcon className = "h-4 w-4 text-primary-500" />
448
482
</ motion . button >
0 commit comments