优化MapSelector

master
LiuJiaNan 2025-12-11 09:14:07 +08:00
parent 611f2535ed
commit db5b5933b2
1 changed files with 5 additions and 7 deletions

View File

@ -19,6 +19,7 @@ const MapSelector = (props) => {
const mapContainerRef = useRef(null); const mapContainerRef = useRef(null);
const mapInstanceRef = useRef(null); const mapInstanceRef = useRef(null);
const local = useRef(null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [currentLongitude, setCurrentLongitude] = useState(longitude || ""); const [currentLongitude, setCurrentLongitude] = useState(longitude || "");
const [currentLatitude, setCurrentLatitude] = useState(latitude || ""); const [currentLatitude, setCurrentLatitude] = useState(latitude || "");
@ -86,10 +87,10 @@ const MapSelector = (props) => {
// 搜索功能 // 搜索功能
const handleLocalSearch = () => { const handleLocalSearch = () => {
if (localSearch && mapInstanceRef.current) { if (localSearch && mapInstanceRef.current) {
const local = new window.BMapGL.LocalSearch(mapInstanceRef.current, { local.current = new window.BMapGL.LocalSearch(mapInstanceRef.current, {
renderOptions: { map: mapInstanceRef.current }, renderOptions: { map: mapInstanceRef.current },
}); });
local.search(localSearch); local.current.search(localSearch);
} }
}; };
@ -97,10 +98,7 @@ const MapSelector = (props) => {
const handleLocalClear = () => { const handleLocalClear = () => {
setLocalSearch(""); setLocalSearch("");
if (mapInstanceRef.current) { if (mapInstanceRef.current) {
const local = new window.BMapGL.LocalSearch(mapInstanceRef.current, { local.current.search("");
renderOptions: { map: mapInstanceRef.current },
});
local.search("");
} }
}; };
@ -206,7 +204,7 @@ const MapSelector = (props) => {
<Button type="primary" onClick={handleLocalSearch}> <Button type="primary" onClick={handleLocalSearch}>
搜索 搜索
</Button> </Button>
<Button onClick={handleLocalClear}> <Button onClick={handleLocalClear} style={{ marginLeft: 8 }}>
重置 重置
</Button> </Button>
</Form.Item> </Form.Item>