Last active
March 27, 2022 08:12
-
-
Save fturkyilmaz/a25f5877e987563e1e94041d8e48311c to your computer and use it in GitHub Desktop.
React Javascript Geolocation API
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
import logo from "./logo.svg"; | |
import "./App.css"; | |
import { useEffect, useState } from "react"; | |
function App() { | |
const [coords, setCoords] = useState({ | |
latitude: null, | |
longitude: null, | |
}); | |
// Geo Location API | |
async function fetchGeoLocation() { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(success, onError); | |
} else { | |
console.log("😢 Tarayıcınız Geolocation desteklemiyor!"); | |
} | |
} | |
function success(position) { | |
const { latitude, longitude } = position.coords; | |
setCoords({ latitude, longitude }); | |
console.log( | |
` 😎 Geo Location Response : Latitude: ${latitude} °, Longitude: ${longitude} °` | |
); | |
} | |
function onError(error) { | |
console.error(`😢 Error : ${error}`); | |
} | |
useEffect(() => { | |
fetchGeoLocation(); | |
}, []); | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<div> | |
<div draggable={true}> | |
<h1>Geolocation API </h1> | |
</div> | |
<p draggable="true"> | |
Latitude: {coords.latitude} °, Longitude: {coords.longitude} °` | |
</p> | |
<h3>Furkan Türkyılmaz</h3> | |
</div> | |
</header> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment