import { Button, Col, Form, Input, Modal, Row, Spin } from "antd"; import { useEffect, useRef, useState } from "react"; const MapSelector = ({ visible, onClose, longitude, latitude, onConfirm, // 新增确认回调 }) => { const mapContainerRef = useRef(null); const mapInstanceRef = useRef(null); const [loading, setLoading] = useState(false); const [currentLongitude, setCurrentLongitude] = useState(longitude || ""); const [currentLatitude, setCurrentLatitude] = useState(latitude || ""); const [localSearch, setLocalSearch] = useState(""); // 当外部经纬度变化时,更新内部状态 useEffect(() => { setCurrentLongitude(longitude || ""); setCurrentLatitude(latitude || ""); }, [longitude, latitude]); // 初始化地图 const initMap = async () => { if (!window.BMapGL) { console.error("BMapGL is not loaded"); return; } setLoading(true); // 确保DOM已经渲染 await new Promise(resolve => setTimeout(resolve, 100)); if (mapContainerRef.current) { // 只有在没有地图实例时才创建新地图 if (!mapInstanceRef.current) { const map = new window.BMapGL.Map(mapContainerRef.current); mapInstanceRef.current = map; map.centerAndZoom( new window.BMapGL.Point( longitude || "119.69457721306945", latitude || "39.940504336846665", ), 16, ); map.enableScrollWheelZoom(true); // 如果有初始坐标,添加标记 if (longitude && latitude) { const point = new window.BMapGL.Point(longitude, latitude); const marker = new window.BMapGL.Marker(point); map.addOverlay(marker); } // 添加点击事件 map.addEventListener("click", (event) => { map.clearOverlays(); const point = new window.BMapGL.Point(event.latlng.lng, event.latlng.lat); const marker = new window.BMapGL.Marker(point); map.addOverlay(marker); setCurrentLatitude(event.latlng.lat); setCurrentLongitude(event.latlng.lng); }); } setLoading(false); } }; // 搜索功能 const handleLocalSearch = () => { if (localSearch && mapInstanceRef.current) { const local = new window.BMapGL.LocalSearch(mapInstanceRef.current, { renderOptions: { map: mapInstanceRef.current }, }); local.search(localSearch); } }; // 关闭弹窗 const handleClose = () => { setLocalSearch(""); if (onClose) onClose(); }; // 确认选择 const handleConfirm = () => { if (onConfirm) { onConfirm(currentLongitude, currentLatitude); } handleClose(); }; // 监听visible变化 useEffect(() => { let initTimer; if (visible) { // 延迟初始化地图,确保DOM完全渲染 initTimer = setTimeout(() => { initMap(); }, 100); } return () => { if (initTimer) { clearTimeout(initTimer); } }; }, [visible]); const handleAfterClose = () => { if (mapInstanceRef.current) { try { mapInstanceRef.current.clearOverlays(); mapInstanceRef.current.destroy(); mapInstanceRef.current = null; } catch (e) { console.warn("Error destroying map on unmount:", e); } mapInstanceRef.current = null; } }; // 组件卸载时清理地图 useEffect(() => { return () => { handleAfterClose(); }; }, []); return (
setLocalSearch(e.target.value)} allowClear />
); }; export default MapSelector;