-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpshunt.html
161 lines (157 loc) · 4.55 KB
/
gpshunt.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<html>
<!-- GPS based puzzle hunt. Show only a number on screen.
That number is the distance in feet from the current position
to the target GPS location. The distance will update as the
player moves until the player is close enough to the target.
Then display a success image and text.
-->
<head>
<style>
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
html, body {
height: 100%;
overflow: hidden;
}
.wrapper {
display: grid;
border-style: solid;
border-color: red;
grid-template-columns: 100%;
grid-template-rows: auto min-content;
grid-gap: 2px;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
#distance-feedback {
//border-style: solid;
//border-color: blue;
grid-column: 1;
grid-row: 1;
font-size: 20vw;
display: flex;
justify-content: center;
align-items: center;
}
#error-feedback {
//border-style: solid;
//border-color: blue;
grid-column: 1;
grid-row: 2;
}
img {
max-width: 100%;
max-height: 100vh; /* Viewport Height */
object-fit: contain;
display: block; /* Removes bottom margin/whitespace */
margin: auto; /* Center the image */
}
/*
html, body { margin: 0px; height: 100%; }
div {
height: 100%;
width: 100%;
border: 2px dashed #4b2869;
}
#distance-feedback {
width:100%;
height:100%;
min-height: 10em;
display: table-cell;
vertical-align: middle;
font-size: 150px;
text-align:center;
}
*/
</style>
<script>
// Example: San Francisco Yoda statue outside Lucasfilm
targetLat='37.7987857';
targetLong='-122.450617';
successImage='https://upload.wikimedia.org/wikipedia/en/f/ff/SuccessKid.jpg';
targetSize = 10; // distance in feet to target for success
// if demoMode is true, ignore the configured target location
// and pick a target approx. 300 feet East of the player's
// starting position.
// (assuming lat. 37.8 N, further or closer at different latitudes)
demoMode = true;
demoModeDistance = 0.0003; // approx. 300 feet East at 38N
const options = {
enableHighAccuracy: true,
timeout: 10000,
};
// unit='M'iles (default) 'K'ilometers 'N'autical miles 'f'eet 'm'eters
function distance(lat1, lon1, lat2, lon2, unit) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K" || unit=="m") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
if (unit=='m') { dist = dist * 1000.0 }
if (unit=='f') { dist = dist * 5280 }
return dist;
}
}
found=false;
function updateDistance(position) {
if (found) {
return;
}
if (demoMode) {
targetLat = position.coords.latitude;
targetLong = position.coords.longitude + demoModeDistance; // approx. 300 feet East at 38N
demoMode = false;
}
dist=distance(targetLat, targetLong, position.coords.latitude, position.coords.longitude, 'f');
console.log(targetLat, targetLong, position.coords.latitude, position.coords.longitude, 'f', dist.toLocaleString(undefined, {maximumFractionDigits: 0}))
//dist=3; // DEBUG
if (dist<targetSize) {
document.getElementById('success-image').src = successImage;
document.getElementById("distance-feedback").innerHTML = '!';
document.getElementById('success-audio').play(); // this won't work on iOS, audio without user pressing a button is forbidden
found=true;
} else {
document.getElementById("distance-feedback").innerHTML = ' ' + dist.toLocaleString(undefined, {maximumFractionDigits: 0}) + ' ';
}
}
function updateError(error) {
document.getElementById("error-feedback").innerHTML = '(Error. You must enable GPS location for this browser)';
}
window.onload = function() {
// do something when the page loads
//document.getElementById("distance-feedback").style.width = "100%";
//document.getElementById("distance-feedback").style.height = "100%";
navigator.geolocation.getCurrentPosition(updateDistance, updateError, options);
const id = navigator.geolocation.watchPosition(updateDistance, updateError, options);
};
</script>
</head>
<body>
<img id='success-image' src="https://www.petertheobald.com/downloads/1x1white.png">
<div class="wrapper">
<div id="distance-feedback">
( )
</div>
<div id="error-feedback">
</div>
</div>
<audio id="success-audio" src="https://www.petertheobald.com/downloads/ta-da.mp3" preload="auto"></audio>
</body>
</html>