-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-current-position.html
executable file
·69 lines (60 loc) · 1.91 KB
/
get-current-position.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
<!-- saved from url=(0066)https://jameshollyer.github.io/api-tests/get-current-position.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>geolocation.getCurrentPosition demo</title>
</head>
<body>
<div id="settings">
Maximum age: <input type="text" id="maximumAge" onchange="onMaxAgeChange()"/>
</div>
<div id="output">
<pre>waiting for getCurrentPosition response</pre>
</div>
<script>
let maximumAgeInput = document.getElementById('maximumAge');
let output = document.getElementById('output');
function onMaxAgeChange() {
let test = parseInt(maximumAgeInput.value);
if(test){
options.maximumAge = test;
}
}
function onSuccess(position) {
console.log(position);
output.innerHTML = `
<pre>
latitude: ${position.coords.latitude}
longitude: ${position.coords.longitude}
altitude: ${position.coords.altitude}
accuracy: ${position.coords.accuracy}
altitudeAccuracy: ${position.coords.altitudeAccuracy}
heading: ${position.coords.heading}
speed: ${position.coords.speed}
timestamp: ${position.timestamp}
</pre>
`;
}
function onError(error) {
console.warn(error);
output.innerHTML =
`<span style="color: red">Error: </span>${error.message} (${error.code})`;
}
options = {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 0
}
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
if(key == "maxAge") {
options.maximumAge = value;
}
});
function getPositionLoop() {
setTimeout(function () {
console.log("getCurrentPosition with these options", options);
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
getPositionLoop();
}, 4000);
}
getPositionLoop();
</script>
</body></html>