@@ -758,7 +758,7 @@ export default function MapComponent() {
758758 // }, []);
759759
760760 // Handle beach search by name or coordinates
761- function handleSearch ( selectedName , lat , lon ) {
761+ function handleSearch ( selectedName , lat , lon , forcePopup = false ) {
762762 if ( ! mapInstanceRef . current ) return ;
763763
764764 // If direct coordinates are provided, use them
@@ -769,6 +769,16 @@ export default function MapComponent() {
769769 const offsetX = 0 ;
770770 const targetLatLng = [ lat , lon ] ;
771771
772+ // Find the marker that matches these coordinates before animation starts
773+ let targetMarker = null ;
774+ markersRef . current . forEach ( ( { marker, lat : markerLat , lon : markerLon } ) => {
775+ // Use a larger tolerance for coordinate matching (0.005 is about 500m)
776+ if ( Math . abs ( markerLat - lat ) < 0.005 && Math . abs ( markerLon - lon ) < 0.005 ) {
777+ console . log ( 'Found matching marker at' , markerLat , markerLon ) ;
778+ targetMarker = marker ;
779+ }
780+ } ) ;
781+
772782 // Calculate offset points for smooth animation
773783 const currentZoom = map . getZoom ( ) ;
774784 const pointAtCurrentZoom = map . project ( targetLatLng , currentZoom ) ;
@@ -779,12 +789,21 @@ export default function MapComponent() {
779789 map . once ( 'moveend' , function ( ) {
780790 // After panning completes, smoothly zoom in
781791 map . once ( 'zoomend' , function ( ) {
782- // Find and open any popup that might be at this location
783- markersRef . current . forEach ( ( { marker, lat : markerLat , lon : markerLon } ) => {
784- if ( Math . abs ( markerLat - lat ) < 0.0001 && Math . abs ( markerLon - lon ) < 0.0001 ) {
785- marker . openPopup ( ) ;
786- }
787- } ) ;
792+ // After zooming completes, open the popup if we found the marker
793+ if ( targetMarker && forcePopup ) {
794+ setTimeout ( ( ) => {
795+ console . log ( 'Opening popup for marker' ) ;
796+ targetMarker . openPopup ( ) ;
797+ } , 400 ) ; // Increased delay to ensure map is settled
798+ } else if ( forcePopup && ! targetMarker ) {
799+ console . log ( 'No marker found to open popup for coordinates:' , lat , lon ) ;
800+
801+ // If we couldn't find a marker but still want to show something at this location
802+ const popup = L . popup ( )
803+ . setLatLng ( [ lat , lon ] )
804+ . setContent ( `<div><strong>${ selectedName || 'Location' } </strong><br>Coordinates: ${ lat . toFixed ( 5 ) } , ${ lon . toFixed ( 5 ) } </div>` )
805+ . openOn ( map ) ;
806+ }
788807 } ) ;
789808
790809 // Animate zoom with a duration in ms
0 commit comments