Skip to content

Instantly share code, notes, and snippets.

@bastienapp
Created May 5, 2021 14:10
Show Gist options
  • Save bastienapp/5ee0f109b9f87536e00c6611599202c6 to your computer and use it in GitHub Desktop.
Save bastienapp/5ee0f109b9f87536e00c6611599202c6 to your computer and use it in GitHub Desktop.
import { Marker, Popup, useMapEvents } from 'react-leaflet';
import { useState, useEffect } from 'react';
function LocationMarker() {
const [position, setPosition] = useState(null);
const map = useMapEvents({
locationfound(e) {
setPosition(e.latlng);
},
});
useEffect(() => {
function locate() {
map.locate();
}
const interval = setInterval(locate, 3000);
return () => clearInterval(interval);
}, [map]);
return position === null ? null : (
<Marker position={position}>
<Popup>You are here</Popup>
</Marker>
);
}
export default LocationMarker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment