1
1
import { useTranslations } from 'next-intl' ;
2
+ import Image from 'next/image' ;
2
3
import React , { useEffect , useState , useCallback , useContext } from 'react' ;
3
4
import { MapContainer , Marker , Popup , TileLayer , useMapEvents } from 'react-leaflet' ;
4
5
import L , { LatLngExpression , LatLngBounds , LatLngTuple } from 'leaflet' ;
@@ -137,6 +138,28 @@ const Map = ({
137
138
logger . debug ( 'Sellers Array:' , { sellers } ) ;
138
139
} , [ sellers ] ) ;
139
140
141
+ // Function to handle marker click
142
+ const handleMarkerClick = ( sellerCoordinates : LatLngTuple ) => {
143
+ if ( ! mapRef . current ) return ;
144
+
145
+ const map = mapRef . current ;
146
+ const currentZoom = map . getZoom ( ) ;
147
+
148
+ // Set the view to the seller's coordinates
149
+ map . setView ( sellerCoordinates , currentZoom , { animate : true } ) ;
150
+ // Get the position of the clicked marker
151
+ const markerPoint = map . latLngToContainerPoint ( sellerCoordinates ) ;
152
+ // Get the width and height of the map container
153
+ const mapSize = map . getSize ( ) ;
154
+ const mapWidth = mapSize . x ;
155
+ const mapHeight = mapSize . y ;
156
+ // Calculate the offsets to center the marker in the map view
157
+ const panOffset = L . point ( mapWidth / 2 - markerPoint . x , mapHeight / 2 - markerPoint . y ) ;
158
+
159
+ // Pan the map by the calculated offset
160
+ map . panBy ( panOffset , { animate : false } ) ; // Disable animation to make the movement instant
161
+ } ;
162
+
140
163
// Function to fetch initial coordinates
141
164
const fetchInitialCoordinates = async ( ) => {
142
165
if ( searchQuery ) return ;
@@ -296,7 +319,12 @@ const Map = ({
296
319
{ isSigningInUser ? (
297
320
< div className = "w-full flex-1 fixed bottom-0 h-[calc(100vh-76.19px)] left-0 right-0 bg-[#f5f1e6] " >
298
321
< div className = "flex justify-center items-center w-full h-full" >
299
- < img src = "/default.png" width = { 120 } height = { 140 } alt = "splashscreen" />
322
+ < Image
323
+ src = "/default.png"
324
+ width = { 120 }
325
+ height = { 140 }
326
+ alt = "splashscreen"
327
+ />
300
328
</ div >
301
329
</ div >
302
330
) : (
@@ -321,11 +349,20 @@ const Map = ({
321
349
position = { seller . coordinates as LatLngExpression }
322
350
key = { seller . seller_id }
323
351
icon = { customIcon }
352
+ eventHandlers = { {
353
+ click : ( ) => handleMarkerClick ( seller . coordinates as LatLngTuple ) ,
354
+ } }
324
355
>
325
- < Popup closeButton = { false } minWidth = { 300 } >
356
+ < Popup
357
+ closeButton = { true }
358
+ minWidth = { 200 }
359
+ maxWidth = { 250 }
360
+ className = "custom-popup"
361
+ offset = { L . point ( 0 , - 3 ) } // Ensures the popup is slightly lower than the marker
362
+ >
326
363
< MapMarkerPopup seller = { seller } />
327
364
</ Popup >
328
- </ Marker >
365
+ </ Marker >
329
366
) ) }
330
367
</ MapContainer >
331
368
) }
0 commit comments