'use client' import { useEffect, useRef, useState } from 'react' import L from 'leaflet' import 'leaflet/dist/leaflet.css' interface MapPickerProps { onSelect: (lat: number, lng: number, address?: string) => void onClose: () => void } export default function MapPicker({ onSelect, onClose }: MapPickerProps) { const mapRef = useRef(null) const markerRef = useRef(null) const mapInstanceRef = useRef(null) const [lat, setLat] = useState(55.7558) const [lng, setLng] = useState(37.6173) const [loading, setLoading] = useState(false) useEffect(() => { if (!mapRef.current || mapInstanceRef.current) return const map = L.map(mapRef.current, { zoomControl: false, attributionControl: false, }).setView([lat, lng], 12) L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', { maxZoom: 19, }).addTo(map) map.on('click', (e) => { const newLat = e.latlng.lat const newLng = e.latlng.lng setLat(newLat) setLng(newLng) if (markerRef.current) markerRef.current.remove() markerRef.current = L.marker([newLat, newLng]).addTo(map) }) mapInstanceRef.current = map return () => { map.remove() mapInstanceRef.current = null } }, []) const handleConfirm = async () => { setLoading(true) try { const res = await fetch( `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lng}&format=json&accept-language=ru` ) const data = await res.json() onSelect(lat, lng, data.display_name || undefined) } catch { onSelect(lat, lng) } } return (
{lat.toFixed(6)}, {lng.toFixed(6)}
) }