-
Notifications
You must be signed in to change notification settings - Fork 5
/
geolocation.html
117 lines (109 loc) · 2.95 KB
/
geolocation.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<html>
<head>
<title>Geolocation demo</title>
<style>
.error {
background-color: lightcoral;
}
.get-current-position {
background-color: aqua;
}
.permission {
background-color: bisque;
}
table {
border-collapse: collapse;
}
table td, table th {
border: 1px solid black;
padding: 3px;
}
table tr:first-child td, table tr:first-child th {
border-top: 0;
}
table tr td:first-child, table tr th:first-child {
border-left: 0;
}
table tr:last-child td, table tr:last-child th {
border-bottom: 0;
}
table tr td:last-child, table tr th:last-child {
border-right: 0;
}
</style>
</head>
<body>
<p>
<button id="watch">Start Watching Position</button> <button id="get">Get Current Position</button>
</p>
<table id="table">
<tr>
<th>Timestamp</th>
<th>Position</th>
<th>Altitude</th>
<th>Heading</th>
<th>Speed</th>
</tr>
</table>
<script>
const table = document.getElementById('table');
function logSuccess(position, fromGetCurrentPosition) {
const row = table.insertRow(1);
if (fromGetCurrentPosition) {
row.classList.add('get-current-position');
}
row.innerHTML = `
<td>${new Date(position.timestamp).toUTCString()}</td>
<td><a target="_blank" href="https://www.google.com/maps/search/${position.coords.latitude},${position.coords.longitude}">${position.coords.latitude}°, ${position.coords.longitude}°</a> ±${position.coords.accuracy} m</td>
<td>${position.coords.altitude} m ±${position.coords.altitudeAccuracy} m</td>
<td>${position.coords.heading}°</td>
<td>${position.coords.speed} m/s</td>
`;
}
function logError(error) {
const row = table.insertRow(1);
row.classList.add('error');
row.innerHTML = `
<td>${new Date().toUTCString()}</td>
<td colspan="4">${error.message} (${error.code})</td>
`;
}
function logPermission(status) {
const row = table.insertRow(1);
row.classList.add('permission');
row.innerHTML = `
<td>${new Date().toUTCString()}</td>
<td colspan="4">Permission status: ${status.state}</td>
`;
}
options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
const watchButton = document.getElementById('watch');
let watchId = -1;
watchButton.addEventListener('click', () => {
if (watchId == -1) {
watchId = navigator.geolocation.watchPosition((position) => {
logSuccess(position, false);
}, logError, options);
watchButton.textContent = 'Stop Watching Position';
} else {
navigator.geolocation.clearWatch(watchId);
watchId = -1;
watchButton.textContent = 'Start Watching Position';
}
});
document.getElementById('get').addEventListener('click', () => {
navigator.geolocation.getCurrentPosition((position) => {
logSuccess(position, true);
}, logError, options)
});
navigator.permissions.query({name: 'geolocation'}).then((status) => {
logPermission(status);
status.addEventListener('change', () => logPermission(status));
});
</script>
</body>
</html>