你好,我希望能够加载地图并使用地理定位坐标。 目前,我的地图已经在一个定义好的中心点上加载,当发生onClick事件时,视图将设置为地理定位位置。 我希望在第一次加载地图时就实现这个效果。 以下是我的代码:
...
const Maps = () => {
// visitor geoLocalisation on the Map
function LocationMarker() {
const [position, setPosition] = useState(null);
const map = useMapEvents({
click() {
map.locate();
},
locationfound(e) {
setPosition(e.latlng);
map.flyTo(e.latlng, map.getZoom());
},
});
return position === null ? null : (
<Marker
position={position}
icon={visitorIcon}
>
<Popup>You are here</Popup>
</Marker>
);
}
return (
<MapContainer
center={[48.856614, 2.3522219]}
zoom={13}
scrollWheelZoom
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<LocationMarker />
</MapContainer>
);
}
at LocationMarker ``` 我查看了React useEffect文档,但我不知道如何创建一个Cleanup函数来取消我的定位函数的订阅。 你能帮我吗?
- emarubimap.stopLocate(); };
但是我仍然收到相同的警告。 - emarubi