-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrent-location.html
70 lines (65 loc) · 2.22 KB
/
current-location.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="./assets/google-maps.png" type="image/x-icon">
<title>Current Location</title>
<style>
a{
text-decoration: none;
}
.button{
padding: 5px;
border: 1px solid #ddd;
background-color: #eee;
}
.playground{
width: 400px;
border: 1px solid #ddd;
margin: 0 auto;
padding: 20px;
box-shadow: 2px 2px 2px #ddd;
}
</style>
</head>
<body>
<div class="playground">
<a class="button" href="./index.html"> ← Back to Home </a>
<h2>Your Current Location</h2>
<p id="location">Getting your location...</p>
</div>
<script>
function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById('location').innerText = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
document.getElementById('location').innerText = `Latitude: ${lat}, Longitude: ${lon}`;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
document.getElementById('location').innerText = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
document.getElementById('location').innerText = "Location information is unavailable.";
break;
case error.TIMEOUT:
document.getElementById('location').innerText = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
document.getElementById('location').innerText = "An unknown error occurred.";
break;
}
}
// Call the function to get the current location
getCurrentLocation();
</script>
</body>
</html>