import React, { useState } from "react"; import { useMap } from "../MapComponent"; import Geolocation from "ol/Geolocation"; import AddRoundedIcon from "@mui/icons-material/AddRounded"; import RemoveRoundedIcon from "@mui/icons-material/RemoveRounded"; import GpsFixedRoundedIcon from "@mui/icons-material/GpsFixedRounded"; import clsx from "clsx"; const INITIAL_ZOOM = 14; // 默认缩放级别 const Zoom: React.FC = () => { const map = useMap(); const [locateDisabled, setLocateDisabled] = useState(false); // 放大函数 const handleZoomIn = () => { if (!map) return; const view = map.getView(); view.animate({ zoom: (view.getZoom() ?? 0) + 1, duration: 200 }); }; // 缩小函数 const handleZoomOut = () => { if (!map) return; const view = map.getView(); view.animate({ zoom: (view.getZoom() ?? 0) - 1, duration: 200 }); }; // 定位功能 const handleLocate = () => { if (!map) return; const geolocation = new Geolocation({ trackingOptions: { enableHighAccuracy: true }, projection: map.getView().getProjection(), }); geolocation.once("change:position", () => { const coords = geolocation.getPosition(); if (coords) { map .getView() .animate({ center: coords, zoom: INITIAL_ZOOM, duration: 500 }); } geolocation.setTracking(false); }); geolocation.setTracking(true); }; // 包装 handleLocate,点击后禁用按钮一段时间 const onLocateClick = () => { navigator.geolocation.getCurrentPosition( () => { handleLocate(); }, (error) => { console.log(error.message); setLocateDisabled(true); // 定位失败后禁用按钮 // alert("定位失败,将使用默认位置。"); } ); }; return (