Skip to content

Instantly share code, notes, and snippets.

@matthieua
Created August 13, 2019 12:41
Show Gist options
  • Save matthieua/8c6d05fea1bb2b27a5f15f683572e716 to your computer and use it in GitHub Desktop.
Save matthieua/8c6d05fea1bb2b27a5f15f683572e716 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Geolocation API</title>
<style>
h1 {
color: #6443ea;
font-family: "PT Mono";
text-align: center;
}
h2 {
font-weight: bold;
font-size: 24px;
text-align: center;
}
button {
display: block;
margin: 0 auto;
background: #6443ea;
color: #fff;
padding: 10px 20px;
}
</style>
</head>
<body>
<h1>🗺 Geolocation API</h1>
<button>
Current Location
</button>
<script>
function showPosition(position) {
let h1 = document.querySelector("h1");
h1.innerHTML = `Your Latitude is ${
position.coords.latitude
} and your longitude is ${position.coords.longitude}`;
}
function getCurrentPosition() {
navigator.geolocation.getCurrentPosition(showPosition);
}
let button = document.querySelector("button");
button.addEventListener("click", getCurrentPosition);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment