@@ -9,7 +9,6 @@ import { ISeller, ISellerWithSettings } from '@/constants/types';
9
9
import { fetchSellers } from '@/services/sellerApi' ;
10
10
11
11
import MapMarkerPopup from './MapMarkerPopup' ;
12
- import { CloseButton } from '../Forms/Buttons/Buttons' ;
13
12
14
13
import { AppContext } from '../../../../context/AppContextProvider' ;
15
14
import logger from '../../../../logger.config.mjs' ;
@@ -88,7 +87,6 @@ const Map = ({
88
87
const [ locationError , setLocationError ] = useState ( false ) ;
89
88
const [ isLocationAvailable , setIsLocationAvailable ] = useState ( false ) ;
90
89
const [ initialLocationSet , setInitialLocationSet ] = useState ( false ) ;
91
- const [ lastClickedMarker , setLastClickedMarker ] = useState < LatLngTuple | null > ( null ) ;
92
90
93
91
useEffect ( ( ) => {
94
92
if ( searchResults . length > 0 ) {
@@ -134,38 +132,34 @@ const Map = ({
134
132
logger . debug ( 'Sellers Array:' , { sellers } ) ;
135
133
} , [ sellers ] ) ;
136
134
137
- useEffect ( ( ) => {
138
- if ( mapRef . current ) {
139
- fetchInitialCoordinates ( ) ; // Fetch sellers when map is ready
140
- }
141
- } , [ mapRef . current ] ) ;
142
-
143
- // Function to handle marker click and center popup
135
+ // Function to handle marker click
144
136
const handleMarkerClick = ( sellerCoordinates : LatLngTuple ) => {
145
137
if ( ! mapRef . current ) return ;
146
138
147
139
const map = mapRef . current ;
148
- setLastClickedMarker ( sellerCoordinates ) ;
149
-
150
- // Set the target zoom level
151
- const targetZoom = 5 ; // Zoom level when clicking the marker
152
-
153
- // Center the map to the marker position without animation
154
- map . setView ( sellerCoordinates , targetZoom , { animate : false } ) ;
140
+ const currentZoom = map . getZoom ( ) ;
155
141
156
- // Calculate offset to center the popup (move right by 2x)
142
+ // Set the view to the seller's coordinates
143
+ map . setView ( sellerCoordinates , currentZoom , { animate : true } ) ;
144
+ // Get the position of the clicked marker
157
145
const markerPoint = map . latLngToContainerPoint ( sellerCoordinates ) ;
146
+ // Get the width and height of the map container
158
147
const mapSize = map . getSize ( ) ;
148
+ const mapWidth = mapSize . x ;
149
+ const mapHeight = mapSize . y ;
150
+ // Calculate the offsets to center the marker in the map view
151
+ const panOffset = L . point ( mapWidth / 2 - markerPoint . x , mapHeight / 2 - markerPoint . y ) ;
159
152
160
- const centerOffset = L . point (
161
- mapSize . x / 2 - markerPoint . x - 40 , // Shift horizontal offset slightly (-40 moves right)
162
- mapSize . y / 2 - markerPoint . y - 229 // Vertical offset unchanged
163
- ) ;
164
-
165
- // Pan the map instantly to center the popup without animation
166
- map . panBy ( centerOffset , { animate : false } ) ;
153
+ // Pan the map by the calculated offset
154
+ map . panBy ( panOffset , { animate : false } ) ; // Disable animation to make the movement instant
167
155
} ;
168
156
157
+ useEffect ( ( ) => {
158
+ if ( mapRef . current ) {
159
+ fetchInitialCoordinates ( ) ; // Fetch sellers when map is ready
160
+ }
161
+ } , [ mapRef . current ] ) ;
162
+
169
163
const saveMapState = ( ) => {
170
164
try {
171
165
if ( ! mapRef . current ) {
@@ -383,27 +377,12 @@ const Map = ({
383
377
} }
384
378
>
385
379
< Popup
386
- minWidth = { 140 }
387
- maxWidth = { 190 }
380
+ closeButton = { true }
381
+ minWidth = { 200 }
382
+ maxWidth = { 250 }
388
383
className = "custom-popup"
389
- offset = { L . point ( 4 , - 0.5 ) } // Shifted offset from [0, -3] to [10, -3] to move the popup right
384
+ offset = { L . point ( 0 , - 3 ) } // Ensures the popup is slightly lower than the marker
390
385
>
391
- < div
392
- style = { {
393
- position : 'absolute' ,
394
- top : '-6px' ,
395
- right : '-6px' ,
396
- zIndex : 1000 ,
397
- } }
398
- >
399
- < CloseButton
400
- onClick = { ( ) => {
401
- mapRef . current ?. closePopup ( ) ; // Close the popup programmatically
402
- } }
403
- aria-label = "Close Popup"
404
- />
405
- </ div >
406
- { /* Popup Content */ }
407
386
< MapMarkerPopup seller = { seller } />
408
387
</ Popup >
409
388
</ Marker >
0 commit comments