11import React , { useEffect , useRef } from 'react' ;
22import { Modal , Platform } from 'react-native' ;
33import { Ionicons } from '@expo/vector-icons' ;
4+ import { useTheme } from '../../../lib/theme' ;
45import { MapModalProps } from './MapModal' ;
56
67// Define types for Leaflet since we're using require instead of import
78type LeafletMap = any ;
89type LeafletMarker = any ;
910
11+ // Leaflet's CSS is now included in index.html
12+
1013export default function MapModal ( {
11- visible ,
14+ isVisible ,
1215 onClose,
1316 onConfirm,
1417 mapRegion,
@@ -24,107 +27,88 @@ export default function MapModal({
2427
2528 // Set up the Leaflet map when the modal becomes visible
2629 useEffect ( ( ) => {
27- if ( ! visible ) return ;
28-
29- const initializeLeafletMap = async ( ) => {
30- try {
31- // Use require instead of dynamic imports to avoid TypeScript module issues
30+ if ( isVisible && Platform . OS === 'web' && webMapRef . current ) {
31+ setTimeout ( ( ) => {
3232 if ( ! leafletRef . current ) {
3333 leafletRef . current = require ( 'leaflet' ) ;
34- // Load the CSS
35- require ( 'leaflet/dist/leaflet.css' ) ;
34+ // require('leaflet/dist/leaflet.css');
3635 }
3736
3837 const L = leafletRef . current ;
39-
40- if ( ! webMapRef . current ) {
41- // If map container doesn't exist yet, wait for it
42- setTimeout ( initializeLeafletMap , 100 ) ;
43- return ;
44- }
45-
46- // Create new map
47- const initialLat = initialCoordinates ?. latitude || mapRegion . latitude ;
48- const initialLng = initialCoordinates ?. longitude || mapRegion . longitude ;
49-
50- const map = L . map ( 'web-map-container' ) . setView (
51- [ initialLat , initialLng ] ,
52- 13
53- ) ;
5438
55- mapInstanceRef . current = map ;
39+ if ( ! mapInstanceRef . current ) {
40+ const map = L . map ( webMapRef . current ) . setView ( [ 51.505 , - 0.09 ] , 13 ) ;
41+
42+ mapInstanceRef . current = map ;
5643
57- L . tileLayer ( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' , {
58- attribution : '© OpenStreetMap contributors'
59- } ) . addTo ( map ) ;
44+ L . tileLayer ( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' , {
45+ attribution : '© OpenStreetMap contributors'
46+ } ) . addTo ( map ) ;
6047
61- // Add a marker at the initial position
62- const marker = L . marker ( [ initialLat , initialLng ] , {
63- draggable : true
64- } ) . addTo ( map ) ;
65-
66- webMarkerRef . current = marker ;
48+ // Add a marker at the initial position
49+ const marker = L . marker ( [ 51.505 , - 0.09 ] , {
50+ draggable : true
51+ } ) . addTo ( map ) ;
52+
53+ webMarkerRef . current = marker ;
6754
68- // Get address on marker drag end
69- marker . on ( 'dragend' , async function ( ) {
70- const position = marker . getLatLng ( ) ;
71- try {
72- const response = await fetch (
73- `https://nominatim.openstreetmap.org/reverse?format=json&lat=${ position . lat } &lon=${ position . lng } &zoom=18&addressdetails=1`
74- ) ;
75- const data = await response . json ( ) ;
76- if ( data && data . display_name ) {
77- setLocationSearchText ( data . display_name ) ;
55+ // Get address on marker drag end
56+ marker . on ( 'dragend' , async function ( ) {
57+ const position = marker . getLatLng ( ) ;
58+ try {
59+ const response = await fetch (
60+ `https://nominatim.openstreetmap.org/reverse?format=json&lat=${ position . lat } &lon=${ position . lng } &zoom=18&addressdetails=1`
61+ ) ;
62+ const data = await response . json ( ) ;
63+ if ( data && data . display_name ) {
64+ setLocationSearchText ( data . display_name ) ;
65+ }
66+ } catch ( err ) {
67+ console . error ( 'Error getting address:' , err ) ;
7868 }
79- } catch ( err ) {
80- console . error ( 'Error getting address:' , err ) ;
81- }
82- } ) ;
69+ } ) ;
8370
84- // Handle map clicks to move marker
85- map . on ( 'click' , async function ( e : { latlng : { lat : number ; lng : number } } ) {
86- const { lat, lng } = e . latlng ;
87- marker . setLatLng ( [ lat , lng ] ) ;
88-
89- try {
90- const response = await fetch (
91- `https://nominatim.openstreetmap.org/reverse?format=json&lat=${ lat } &lon=${ lng } &zoom=18&addressdetails=1`
92- ) ;
93- const data = await response . json ( ) ;
94- if ( data && data . display_name ) {
95- setLocationSearchText ( data . display_name ) ;
71+ // Handle map clicks to move marker
72+ map . on ( 'click' , async function ( e : { latlng : { lat : number ; lng : number } } ) {
73+ const { lat, lng } = e . latlng ;
74+ marker . setLatLng ( [ lat , lng ] ) ;
75+
76+ try {
77+ const response = await fetch (
78+ `https://nominatim.openstreetmap.org/reverse?format=json&lat=${ lat } &lon=${ lng } &zoom=18&addressdetails=1`
79+ ) ;
80+ const data = await response . json ( ) ;
81+ if ( data && data . display_name ) {
82+ setLocationSearchText ( data . display_name ) ;
83+ }
84+ } catch ( err ) {
85+ console . error ( 'Error getting address:' , err ) ;
9686 }
97- } catch ( err ) {
98- console . error ( 'Error getting address:' , err ) ;
99- }
100- } ) ;
87+ } ) ;
10188
102- // Set up search control event handling
103- const setupSearchControl = ( ) => {
104- const searchControl = document . getElementById ( 'web-map-search' ) ;
105- if ( searchControl ) {
106- searchControl . addEventListener ( 'keypress' , handleSearchKeyPress ) ;
107- } else {
108- // Try again if the search control isn't available yet
109- setTimeout ( setupSearchControl , 100 ) ;
110- }
111- } ;
89+ // Set up search control event handling
90+ const setupSearchControl = ( ) => {
91+ const searchControl = document . getElementById ( 'web-map-search' ) ;
92+ if ( searchControl ) {
93+ searchControl . addEventListener ( 'keypress' , handleSearchKeyPress ) ;
94+ } else {
95+ // Try again if the search control isn't available yet
96+ setTimeout ( setupSearchControl , 100 ) ;
97+ }
98+ } ;
11299
113- setupSearchControl ( ) ;
100+ setupSearchControl ( ) ;
114101
115- return ( ) => {
116- if ( mapInstanceRef . current ) {
117- mapInstanceRef . current . remove ( ) ;
118- mapInstanceRef . current = null ;
119- }
120- } ;
121- } catch ( error ) {
122- console . error ( 'Error initializing Leaflet map:' , error ) ;
123- }
124- } ;
125-
126- initializeLeafletMap ( ) ;
127- } , [ visible , initialCoordinates , mapRegion . latitude , mapRegion . longitude ] ) ;
102+ return ( ) => {
103+ if ( mapInstanceRef . current ) {
104+ mapInstanceRef . current . remove ( ) ;
105+ mapInstanceRef . current = null ;
106+ }
107+ } ;
108+ }
109+ } , 100 ) ;
110+ }
111+ } , [ isVisible , initialCoordinates , mapRegion . latitude , mapRegion . longitude ] ) ;
128112
129113 const handleSearchKeyPress = async ( e : KeyboardEvent ) => {
130114 if ( e . key === 'Enter' ) {
@@ -187,7 +171,7 @@ export default function MapModal({
187171 < Modal
188172 animationType = "slide"
189173 transparent = { false }
190- visible = { visible }
174+ visible = { isVisible }
191175 onRequestClose = { onClose } >
192176 < div style = { {
193177 position : 'fixed' ,
0 commit comments