Skip to content

Instantly share code, notes, and snippets.

@sanatem
Created May 9, 2023 16:09
Show Gist options
  • Save sanatem/4ac7bfad28472f4ea44a2b5377aa2fd0 to your computer and use it in GitHub Desktop.
Save sanatem/4ac7bfad28472f4ea44a2b5377aa2fd0 to your computer and use it in GitHub Desktop.
Geolocation React example
const [coords, setCoords] = useState({});
useEffect(() => {
navigator.geolocation.getCurrentPosition(
position => {
const { latitude, longitude } = position.coords;
setCoords({ lat: latitude, lng: longitude });
},
error => {
console.log(error.message);
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment