import React, { useEffect, useRef, useState } from "react"; import { Marker, useMapEvents } from "react-leaflet"; import "leaflet/dist/leaflet.css"; import L from "leaflet"; import { Alert, Box, Button, Stack, Typography, Zoom } from "@mui/material"; import dynamic from "next/dynamic"; import MapLoading from "@/core/components/MapLayer/Loading"; import EndIcon from "@/assets/images/examine_marker_active.png"; import StartIcon from "@/assets/images/examine_marker.png"; const MapLayer = dynamic(() => import("@/core/components/MapLayer"), { loading: () => , ssr: false, }); const createCustomIcon = (size, iconUrl, labelText, color) => { if (labelText) { return L.divIcon({ className: "custom-marker", // Apply your custom CSS class html: `
${labelText}
`, iconSize: [50, 50], iconAnchor: [25, 50], }); } return L.icon({ iconUrl: iconUrl, iconSize: size, iconAnchor: [size[0] / 2, size[1]], popupAnchor: [0, -size[1]], }); }; const MAX_ZOOM_FOR_MARKER = 13; const MapInteraction = ({ setValue, startLat, startLng, endLat, endLng }) => { const [isStartLocked, setIsStartLocked] = useState(!!(startLat && startLng)); const [isEndLocked, setIsEndLocked] = useState(!!(endLat && endLng)); const [startPosition, setStartPosition] = useState(startLat && startLng ? { lat: startLat, lng: startLng } : null); const [endPosition, setEndPosition] = useState(endLat && endLng ? { lat: endLat, lng: endLng } : null); const [enableSend, setEnableSend] = useState(false); const [startIconColor, setStartIconColor] = useState(startLat ? "#1CAC66" : "#003d4f"); const [endIconColor, setEndIconColor] = useState(endLat ? "#D13131" : "#003d4f"); const startRef = useRef(); const endRef = useRef(); const map = useMapEvents({ move(e) { setEnableSend(e.target.getZoom() > MAX_ZOOM_FOR_MARKER); if (!isStartLocked && startRef.current) { startRef.current.setLatLng(e.target.getCenter()); } else if (isStartLocked && !isEndLocked && endRef.current) { endRef.current.setLatLng(e.target.getCenter()); } }, zoom(e) { setEnableSend(e.target.getZoom() > MAX_ZOOM_FOR_MARKER); if (!isStartLocked && startRef.current) { startRef.current.setLatLng(e.target.getCenter()); } else if (isStartLocked && !isEndLocked && endRef.current) { endRef.current.setLatLng(e.target.getCenter()); } }, }); useEffect(() => { if (startLat && endLng) { map.fitBounds( [ { lat: startLat, lng: startLng }, { lat: endLat, lng: endLng }, ], { paddingTopLeft: [64, 64], paddingBottomRight: [64, 64] } ); } }, [startLat, map, endLng]); const handleUnlockStart = () => { setIsStartLocked(false); setStartPosition(null); setValue("start_point", null); setStartIconColor("#003d4f"); }; const handleUnlockEnd = () => { setIsEndLocked(false); setIsStartLocked(false); setEndPosition(null); setStartPosition(null); setValue("end_point", ""); setValue("start_point", null); setStartIconColor("#003d4f"); setEndIconColor("#003d4f"); }; return ( <> { if (!enableSend) return; if (!isStartLocked) { const center = map.getCenter(); setValue("start_point", { lat: center.lat.toString(), lng: center.lng.toString() }); setStartPosition({ lat: center.lat, lng: center.lng }); setIsStartLocked(true); setStartIconColor("#1CAC66"); map.panBy([25, 25]); } }, }} /> برای ثبت محل فعالیت، لطفاً نقشه را بیشتر زوم کنید. {isStartLocked && !isEndLocked && ( )} {isStartLocked && ( <> برای ثبت محل فعالیت، لطفاً نقشه را بیشتر زوم کنید. { if (!enableSend) return; if (!isEndLocked) { const center = map.getCenter(); setValue("end_point", { lat: center.lat.toString(), lng: center.lng.toString() }); setEndPosition({ lat: center.lat, lng: center.lng }); setIsEndLocked(true); setEndIconColor("#D13131"); map.fitBounds([startPosition, map.getCenter()], { paddingTopLeft: [64, 64], paddingBottomRight: [64, 64], }); } }, }} /> )} {isEndLocked && ( )} ); }; const MapInfoTwoMarker = ({ setValue, errors, StartPoint = null, EndPoint = null }) => { return ( {errors.start_point && ( {errors.start_point.message} )} {errors.end_point && ( {errors.end_point.message} )} ); }; export default MapInfoTwoMarker;