-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f08ae9c
commit 807383b
Showing
3 changed files
with
62 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { latLngToCell } from "h3-js"; | ||
import React, { useCallback, useState } from "react"; | ||
|
||
export const WhereAmIButton = ({ setUserInput }) => { | ||
const [geolocationStatus, setGeolocationStatus] = useState(""); | ||
|
||
const doGeoLocation = useCallback(async () => { | ||
if ("geolocation" in navigator) { | ||
setGeolocationStatus("Locating..."); | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => { | ||
// TODO: Consider using the accuracy to select resolution | ||
setUserInput( | ||
latLngToCell( | ||
position.coords.latitude, | ||
position.coords.longitude, | ||
11, | ||
), | ||
); | ||
setGeolocationStatus(""); | ||
}, | ||
() => { | ||
setGeolocationStatus("Error"); | ||
}, | ||
); | ||
} else { | ||
setGeolocationStatus("No location services"); | ||
} | ||
}, [setUserInput]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
position: "absolute", | ||
right: "12px", | ||
top: "12px", | ||
}} | ||
> | ||
<button | ||
type="button" | ||
onClick={doGeoLocation} | ||
title="Where am I?" | ||
disabled={Boolean(geolocationStatus)} | ||
> | ||
{geolocationStatus || ( | ||
<img | ||
style={{ filter: "grayscale(1)" }} | ||
src="images/icon-high-precision.svg" | ||
alt="Where am I?" | ||
title="Where am I?" | ||
></img> | ||
)} | ||
</button> | ||
</div> | ||
); | ||
}; |