Created
August 7, 2021 16:26
-
-
Save ShahStavan/61a402d044c5789796403306ee91f30c to your computer and use it in GitHub Desktop.
Know Your Geolocation using a Navigator.Geolocation script
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Know Your Geolocation</title> | |
</head> | |
<body> | |
<h1>Geolocation</h1> | |
<p>Latitude: <span id="latitude"></span><span>°</span><br> | |
Longitude: <span id="longitude"></span> <span>°</span> | |
</p> | |
<script> | |
//Special thanks to MDN Web docs✌️ | |
if('geolocation' in navigator) { | |
console.log("Geolocation is available"); | |
navigator.geolocation.getCurrentPosition(function (position) { | |
console.log(position); | |
const lat = position.coords.latitude; | |
document.getElementById('latitude').textContent = lat; | |
const lon = position.coords.longitude; | |
document.getElementById('longitude').textContent=lon; | |
}); | |
} else { | |
console.log("Geolocation is not available!"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment