Skip to content

Instantly share code, notes, and snippets.

@ShahStavan
Created August 7, 2021 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShahStavan/61a402d044c5789796403306ee91f30c to your computer and use it in GitHub Desktop.
Save ShahStavan/61a402d044c5789796403306ee91f30c to your computer and use it in GitHub Desktop.
Know Your Geolocation using a Navigator.Geolocation script
<!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>&deg;</span><br>
Longitude: <span id="longitude"></span> <span>&deg;</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