1
1
import { useTranslations } from 'next-intl' ;
2
2
import Image from 'next/image' ;
3
- import React , { useEffect , useState , useCallback , useContext } from 'react' ;
3
+ import React , { useEffect , useState , useCallback , useContext , useRef } from 'react' ;
4
4
import { MapContainer , Marker , Popup , TileLayer , useMapEvents } from 'react-leaflet' ;
5
5
import L , { LatLngExpression , LatLngBounds , LatLngTuple } from 'leaflet' ;
6
6
import _ from 'lodash' ;
@@ -132,39 +132,44 @@ const Map = ({
132
132
logger . debug ( 'Sellers Array:' , { sellers } ) ;
133
133
} , [ sellers ] ) ;
134
134
135
- // Function to handle marker click
136
- const handleMarkerClick = ( sellerCoordinates : LatLngTuple ) => {
137
- if ( ! mapRef . current ) return ;
135
+ const useMarkerZoomHandler = ( mapRef : React . RefObject < L . Map > ) => {
136
+ const lastClickedMarker = useRef < string | null > ( null ) ;
138
137
139
- const map = mapRef . current ;
140
- const currentZoom = map . getZoom ( ) ;
141
- const maxZoom = map . getMaxZoom ( ) ;
142
-
143
- // Check if zoom & pan have already been applied
144
- const hasZoomed = mapRef . current ?. getContainer ( ) . dataset . zoomApplied === "true" ;
145
-
146
- if ( hasZoomed ) return ; // Prevent further zoom & pan on second click
138
+ // Function to handle marker click
139
+ const handleMarkerClick = ( sellerCoordinates : LatLngTuple ) => {
140
+ if ( ! mapRef . current ) return ;
147
141
148
- // Apply zoom increase only once
149
- const targetZoom = Math . min ( currentZoom + 3 , maxZoom ) ;
150
- const zoomLevel = targetZoom ;
142
+ const map = mapRef . current ;
143
+ const currentZoom = map . getZoom ( ) ;
144
+ const maxZoom = map . getMaxZoom ( ) ;
151
145
152
- // Convert marker lat/lng to pixel position
153
- const markerPoint = map . latLngToContainerPoint ( sellerCoordinates ) ;
146
+ const coordKey = sellerCoordinates . join ( ',' ) ;
154
147
155
- // Adjust popup position: move **left (-X) and up (-Y)**
156
- const offsetX = - 3 ; // Slight left shift
157
- const offsetY = 28 ; // Moves up instead of down
158
- const panOffset = L . point ( offsetX , offsetY ) ;
148
+ // Prevent zooming again on the same marker
149
+ if ( lastClickedMarker . current === coordKey ) return ;
159
150
160
- // Calculate new center position based on offset
161
- const newCenter = map . containerPointToLatLng ( markerPoint . subtract ( panOffset ) ) ;
151
+ // Update the last clicked marker
152
+ lastClickedMarker . current = coordKey ;
162
153
163
- // Apply view changes **only once**
164
- map . setView ( newCenter , zoomLevel , { animate : false } ) ;
154
+ // Calculate target zoom
155
+ const targetZoom = Math . min ( currentZoom + 3 , maxZoom ) ;
165
156
166
- // Mark zoom as applied (prevents further zooming & movement)
167
- mapRef . current . getContainer ( ) . dataset . zoomApplied = "true" ;
157
+ // Convert lat/lng to pixel position
158
+ const markerPoint = map . latLngToContainerPoint ( sellerCoordinates ) ;
159
+
160
+ // Offset to move popup up and left
161
+ const OFFSET_X = - 3 ;
162
+ const OFFSET_Y = 28 ;
163
+ const panOffset = L . point ( OFFSET_X , OFFSET_Y ) ;
164
+
165
+ // New center for map
166
+ const newCenter = map . containerPointToLatLng ( markerPoint . subtract ( panOffset ) ) ;
167
+
168
+ // Zoom and pan with animation
169
+ map . setView ( newCenter , targetZoom , { animate : true } ) ;
170
+ } ;
171
+
172
+ return handleMarkerClick ;
168
173
} ;
169
174
170
175
useEffect ( ( ) => {
@@ -173,6 +178,8 @@ const Map = ({
173
178
}
174
179
} , [ mapRef . current ] ) ;
175
180
181
+ const handleMarkerClick = useMarkerZoomHandler ( mapRef ) ;
182
+
176
183
const saveMapState = ( ) => {
177
184
try {
178
185
if ( ! mapRef . current ) {
0 commit comments