Skip to content

Instantly share code, notes, and snippets.

@Magnacarter
Created March 6, 2022 06:02
Show Gist options
  • Save Magnacarter/ab39900d89fae0ff85c3a7a65507ba4a to your computer and use it in GitHub Desktop.
Save Magnacarter/ab39900d89fae0ff85c3a7a65507ba4a to your computer and use it in GitHub Desktop.
React hook for getting a user's latlong from their browser.
import { useState, useContext } from 'react'
import { ACTION_TYPES, StoreContext } from '../pages/_app'
const useTrackLocation = () => {
const [locationErrorMsg, setLocationErrorMsg] = useState('');
//const [latlong, setLatLong] = useState('');
const [isFindingLocation, setIsFindingLoaction] = useState(false);
const { dispatch } = useContext(StoreContext);
const success = (position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// setLatLong(`${latitude},${longitude}`);
// With context we have a global state for latlong.
// Dispatch is comming from _api.js in the <StoreProdider/>
dispatch({
type: ACTION_TYPES.SET_LATLONG,
payload: { latlong: `${latitude},${longitude}` }
});
setLocationErrorMsg('');
setIsFindingLoaction(false);
}
const error = () => {
setIsFindingLoaction(false);
setLocationErrorMsg('Unable to retrieve your location');
}
const handleTrackLocation = () => {
setIsFindingLoaction(true);
if(!navigator.geolocation) {
setLocationErrorMsg('Geolocation is not supported by your browser');
setIsFindingLoaction(false);
} else {
navigator.geolocation.getCurrentPosition(success, error);
}
}
return {
//latlong,
handleTrackLocation,
locationErrorMsg,
isFindingLocation
}
}
export default useTrackLocation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment