import React, {useCallback, useEffect, useState} from 'react' import 'leaflet' import 'leaflet/dist/leaflet.css' import { LayersControl, MapContainer, //Marker, //Polygon, //Polyline, Popup, Circle, TileLayer, useMap, useMapEvent } from '@monsonjeremy/react-leaflet' import * as L from 'leaflet' import { useLeafletStore } from './LeafletStore' type LeafletMapProps = {onBoundsChange?: (bounds: L.LatLngBounds) => void} const BoundsChangeListener = ({onBoundsChange}: {onBoundsChange?: (bounds: L.LatLngBounds) => void}) => { const { setCenter} = useLeafletStore() const map = useMap() const updateBounds = useCallback( () => { setCenter(map.getCenter()) // onBoundsChange is unused at the moment onBoundsChange && onBoundsChange( map.getBounds() ) }, [map, onBoundsChange, setCenter], ) useEffect(() => { updateBounds() }, [map, updateBounds]) useMapEvent('moveend', (e) => updateBounds()) useMapEvent('load', (e) => updateBounds()) return null } const LeafletMap = ({onBoundsChange}: LeafletMapProps) => { const [zoom, setZoom] = useState( 8 ) const [position, setPosition] = useState( { lat: 51.0833, lng: 13.73126, } ) const leafletStore = useLeafletStore() return ( <> {leafletStore.markers.map((m, i) => /** TODO: Maybe a clustered marker would be helpfull, but we loose the possibility of showing the radius (display accuracy of the coordinate). * Probably the best solution is showing Circle and clustered marker. **/ { m.content } )} {leafletStore.filteredMarkers.map((m, i) => /** TODO: Maybe a clustered marker would be helpfull, but we loose the possibility of showing the radius (display accuracy of the coordinate). * Probably the best solution is showing Circle and clustered marker. **/ { m.content } )} ) } export default LeafletMap