@@ -53,14 +53,20 @@ const DEFAULT_COORDINATES = {
5353 zoom : 9 ,
5454} ;
5555
56+ function getListingCoordinates ( listing ) {
57+ return listing ?. coordinates ?? null ;
58+ }
59+
5660function hasValidCoordinates ( listing ) {
61+ const coordinates = getListingCoordinates ( listing ) ;
62+
5763 return (
5864 listing &&
5965 ! listing . error &&
60- typeof listing . latitude === "number" &&
61- typeof listing . longitude === "number" &&
62- Number . isFinite ( listing . latitude ) &&
63- Number . isFinite ( listing . longitude )
66+ typeof coordinates ? .latitude === "number" &&
67+ typeof coordinates ? .longitude === "number" &&
68+ Number . isFinite ( coordinates . latitude ) &&
69+ Number . isFinite ( coordinates . longitude )
6470 ) ;
6571}
6672
@@ -86,7 +92,9 @@ export default function MapImmersive({
8692 isDesktop,
8793 countryCode,
8894} ) {
95+ const selectedListingCoordinates = getListingCoordinates ( selectedListing ) ;
8996 const isFirstLoad = useRef ( true ) ;
97+ const hasCenteredSelectedListingRef = useRef ( false ) ;
9098 const [ lastKnownPosition , setLastKnownPosition ] = useState ( null ) ;
9199 const [ isListingInView , setIsListingInView ] = useState ( true ) ;
92100 const hasInitialPosition =
@@ -111,11 +119,14 @@ export default function MapImmersive({
111119
112120 // If there's a selected listing with valid coords, center on it instead of using IP location
113121 if ( hasValidCoordinates ( selectedListing ) ) {
122+ const coordinates = getListingCoordinates ( selectedListing ) ;
123+
114124 mapRef . current ?. flyTo ( {
115- center : [ selectedListing . longitude , selectedListing . latitude ] ,
125+ center : [ coordinates . longitude , coordinates . latitude ] ,
116126 zoom : 12 ,
117127 duration : 0 ,
118128 } ) ;
129+ hasCenteredSelectedListingRef . current = true ;
119130 }
120131
121132 const bounds = mapRef . current . getMap ( ) . getBounds ( ) ;
@@ -133,9 +144,10 @@ export default function MapImmersive({
133144
134145 // Check if selected listing is in view (only when it has valid coords)
135146 if ( hasValidCoordinates ( selectedListing ) ) {
147+ const coordinates = getListingCoordinates ( selectedListing ) ;
136148 const isInView = bounds . contains ( [
137- selectedListing . longitude ,
138- selectedListing . latitude ,
149+ coordinates . longitude ,
150+ coordinates . latitude ,
139151 ] ) ;
140152 setIsListingInView ( isInView ) ;
141153 }
@@ -144,8 +156,10 @@ export default function MapImmersive({
144156 const handleFlyToListing = useCallback ( ( ) => {
145157 if ( ! hasValidCoordinates ( selectedListing ) || ! mapRef . current ) return ;
146158
159+ const coordinates = getListingCoordinates ( selectedListing ) ;
160+
147161 mapRef . current . flyTo ( {
148- center : [ selectedListing . longitude , selectedListing . latitude ] ,
162+ center : [ coordinates . longitude , coordinates . latitude ] ,
149163 duration : 1500 ,
150164 } ) ;
151165 } , [ selectedListing ] ) ;
@@ -154,32 +168,52 @@ export default function MapImmersive({
154168 let protocol = new Protocol ( ) ;
155169 maplibregl . addProtocol ( "pmtiles" , protocol . tile ) ;
156170
157- // Only handle initial positioning on first load
158- if ( isFirstLoad . current ) {
159- isFirstLoad . current = false ;
160-
161- // If there's a selected listing in URL with valid coords, center on it
162- if ( hasValidCoordinates ( selectedListing ) ) {
163- mapRef . current ?. flyTo ( {
164- center : [ selectedListing . longitude , selectedListing . latitude ] ,
165- zoom : 12 ,
166- duration : 0 ,
167- } ) ;
168- }
169- // If no listing but we have IP coordinates, use those
170- else if ( initialCoordinates ) {
171- mapRef . current ?. flyTo ( {
172- center : [ initialCoordinates . longitude , initialCoordinates . latitude ] ,
173- zoom : initialCoordinates . zoom ,
174- duration : 0 ,
175- } ) ;
176- }
177- }
178-
179171 return ( ) => {
180172 maplibregl . removeProtocol ( "pmtiles" ) ;
181173 } ;
182- } , [ ] ) ; // Empty dependency array as before
174+ } , [ ] ) ;
175+
176+ useEffect ( ( ) => {
177+ if (
178+ ! isFirstLoad . current ||
179+ selectedListing ||
180+ ! initialCoordinates ||
181+ ! mapRef . current
182+ ) {
183+ return ;
184+ }
185+
186+ isFirstLoad . current = false ;
187+ mapRef . current . flyTo ( {
188+ center : [ initialCoordinates . longitude , initialCoordinates . latitude ] ,
189+ zoom : initialCoordinates . zoom ,
190+ duration : 0 ,
191+ } ) ;
192+ } , [ initialCoordinates , mapRef , selectedListing ] ) ;
193+
194+ useEffect ( ( ) => {
195+ if (
196+ ! mapRef . current ||
197+ ! hasValidCoordinates ( selectedListing ) ||
198+ hasCenteredSelectedListingRef . current
199+ ) {
200+ return ;
201+ }
202+
203+ const coordinates = getListingCoordinates ( selectedListing ) ;
204+
205+ mapRef . current . flyTo ( {
206+ center : [ coordinates . longitude , coordinates . latitude ] ,
207+ zoom : 12 ,
208+ duration : 0 ,
209+ } ) ;
210+ hasCenteredSelectedListingRef . current = true ;
211+ } , [
212+ mapRef ,
213+ selectedListing ,
214+ selectedListingCoordinates ?. latitude ,
215+ selectedListingCoordinates ?. longitude ,
216+ ] ) ;
183217
184218 // Set mapController to set relationship between MapSearch and MapImmersive
185219 // Can't get this to work, perhaps delete all mapController and createMapLibreGlMapController code if I can't get it working
@@ -193,9 +227,11 @@ export default function MapImmersive({
193227 // Update lastKnownPosition when we have a valid position
194228 useEffect ( ( ) => {
195229 if ( hasValidCoordinates ( selectedListing ) ) {
230+ const coordinates = getListingCoordinates ( selectedListing ) ;
231+
196232 setLastKnownPosition ( {
197- latitude : selectedListing . latitude ,
198- longitude : selectedListing . longitude ,
233+ latitude : coordinates . latitude ,
234+ longitude : coordinates . longitude ,
199235 } ) ;
200236 } else if ( initialCoordinates && ! lastKnownPosition ) {
201237 setLastKnownPosition ( initialCoordinates ) ;
@@ -210,9 +246,10 @@ export default function MapImmersive({
210246 }
211247
212248 const bounds = mapRef . current . getMap ( ) . getBounds ( ) ;
249+ const coordinates = getListingCoordinates ( selectedListing ) ;
213250 const isInView = bounds . contains ( [
214- selectedListing . longitude ,
215- selectedListing . latitude ,
251+ coordinates . longitude ,
252+ coordinates . latitude ,
216253 ] ) ;
217254 console . log ( "isInView" , isInView ) ;
218255 setIsListingInView ( isInView ) ;
@@ -269,13 +306,13 @@ export default function MapImmersive({
269306 initialViewState = { {
270307 longitude :
271308 ( hasValidCoordinates ( selectedListing )
272- ? selectedListing . longitude
309+ ? selectedListingCoordinates . longitude
273310 : null ) ??
274311 initialCoordinates ?. longitude ??
275312 DEFAULT_COORDINATES . longitude ,
276313 latitude :
277314 ( hasValidCoordinates ( selectedListing )
278- ? selectedListing . latitude
315+ ? selectedListingCoordinates . latitude
279316 : null ) ??
280317 initialCoordinates ?. latitude ??
281318 DEFAULT_COORDINATES . latitude ,
@@ -308,8 +345,8 @@ export default function MapImmersive({
308345 . map ( ( listing ) => (
309346 < DrawerTrigger key = { listing . id } >
310347 < Marker
311- longitude = { listing . longitude }
312- latitude = { listing . latitude }
348+ longitude = { listing . coordinates . longitude }
349+ latitude = { listing . coordinates . latitude }
313350 anchor = "center"
314351 onClick = { ( event ) => {
315352 event . originalEvent . stopPropagation ( ) ;
0 commit comments