Created
May 5, 2021 14:10
-
-
Save bastienapp/5ee0f109b9f87536e00c6611599202c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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