Skip to content

Commit ff93aae

Browse files
authored
Merge pull request #269 from map-of-pi/bug/restore-map-marker-popup
Self approved; restore map marker popup changes.
2 parents 090c279 + 3dc4eed commit ff93aae

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/components/shared/map/Map.tsx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useTranslations } from 'next-intl';
2+
import Image from 'next/image';
23
import React, { useEffect, useState, useCallback, useContext } from 'react';
34
import { MapContainer, Marker, Popup, TileLayer, useMapEvents } from 'react-leaflet';
45
import L, { LatLngExpression, LatLngBounds, LatLngTuple } from 'leaflet';
@@ -137,6 +138,28 @@ const Map = ({
137138
logger.debug('Sellers Array:', { sellers });
138139
}, [sellers]);
139140

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+
140163
// Function to fetch initial coordinates
141164
const fetchInitialCoordinates = async () => {
142165
if (searchQuery) return;
@@ -296,7 +319,12 @@ const Map = ({
296319
{isSigningInUser ? (
297320
<div className="w-full flex-1 fixed bottom-0 h-[calc(100vh-76.19px)] left-0 right-0 bg-[#f5f1e6] ">
298321
<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+
/>
300328
</div>
301329
</div>
302330
) : (
@@ -321,11 +349,20 @@ const Map = ({
321349
position={seller.coordinates as LatLngExpression}
322350
key={seller.seller_id}
323351
icon={customIcon}
352+
eventHandlers={{
353+
click: () => handleMarkerClick(seller.coordinates as LatLngTuple),
354+
}}
324355
>
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+
>
326363
<MapMarkerPopup seller={seller} />
327364
</Popup>
328-
</Marker>
365+
</Marker>
329366
))}
330367
</MapContainer>
331368
)}

0 commit comments

Comments
 (0)