-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
357 lines (305 loc) · 13.1 KB
/
index.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* MODIFY THIS FOR A DIFFERENT TARGET LOCATION
* - your target (for what will show up in the pointer, keep it short)
* - your descriptor (what will show up for "Find <x>" in the title/button)
* - the location (lat, lon to as specific as you can make it)
*/
$target = 'Barn';
$descriptor = 'the barn';
$location = ['lat' => 43.70720223386053, 'lon' => -72.29280809595264];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Favicon -->
<link rel="icon" type="image/png" href="./favicon/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="./favicon/favicon.svg" />
<link rel="shortcut icon" href="./favicon/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="./favicon/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="<?php echo $target; ?>" />
<link rel="manifest" href="./favicon/site.webmanifest" />
<meta name="theme-color" content="#053f2e">
<!-- Meta tags -->
<meta name="robots" content="index, follow, archive">
<meta name="description" content="Find <?php echo $descriptor; ?>">
<meta charset="utf-8" />
<meta http-equiv="Cache-control" content="public">
<!-- SEO and Semantic Markup -->
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="@alex_beals">
<meta property="og:title" content="Find <?php echo $descriptor; ?>">
<meta property="og:type" content="website">
<meta property="og:url" content="https://alexbeals.com/projects/barn/">
<meta property="og:description" content="Which direction is <?php echo $descriptor; ?>?">
<title>Find <?php echo $descriptor; ?></title>
<?php
// Respects 'Request Desktop Site'
if (preg_match("/(iPhone|iPod|iPad|Android|BlackBerry)/i", $_SERVER["HTTP_USER_AGENT"])) {
?><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, viewport-fit=cover"><?php
}
?>
<style>
html {
height: 100%;
background: #053f2e; /* fallback for old browsers */
background: -webkit-linear-gradient(to top, #243124, #053f2e); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to top, #243124, #053f2e); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
text-align: center;
font-size: 16px;
margin: 0px;
height: 100%;
}
body, html { overscroll-behavior: none; }
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
}
.hole {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 235px;
height: 235px;
border-radius: 50%;
background-color: rgb(217, 255, 110, 13%);
margin-top: 50px;
}
.plus {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -55%);
color: #d9ff6e;
font-size: 3em;
font-weight: 100;
display: none;
}
.pin {
position: absolute;
top: -7px;
}
#target {
position: absolute;
top: 0px;
width: 100%;
height: 100%;
}
#target svg {
position: absolute;
left: calc(50% - 20px);
top: -10px;
}
#target span {
position: relative;
top: calc(50% - 1em - 1px);
font-weight: bold;
color: #d9ff6e;
padding: 0 2px;
z-index: 2;
border-radius: 9px;
}
#compass {
position: relative;
width: 210px;
height: 210px;
border-radius: 50%;
background-color: #053f2ecc;
color: #d9ff6e;
}
#compass #n {
position: absolute;
top: 5px;
left: 50%;
transform: translateX(-50%);
font-weight: bold;
}
#compass #s {
position: absolute;
bottom: 5px;
left: 50%;
transform: translateX(-50%);
}
#compass #w {
position: absolute;
top: 50%;
left: 5px;
transform: translateY(-50%);
}
#compass #e {
position: absolute;
top: 50%;
right: 5px;
transform: translateY(-50%);
}
.header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-evenly;
color: #d9ff6e;
width: 100vw;
}
.header div {
display: flex;
flex-direction: column;
}
.header .title {
font-size: 1em;
padding-bottom: 4px;
}
.header .value {
font-size: 2em;
}
</style>
<script>
/** Constants (or ones that will be derived once) */
let UPDATE_EVENTS = true;
const TARGET = <?php echo '{"lat": ' . $location['lat'] . ', "lon": ' . $location['lon'] . '};'; ?>
let compass, targetSpan;
// From https://www.movable-type.co.uk/scripts/latlong.html
function getDistance(lat1, lon1, lat2, lon2) {
const R = 3958.8; // earth radius in miles
const φ1 = lat1 * Math.PI/180; // φ, λ in radians
const φ2 = lat2 * Math.PI/180;
const Δφ = (lat2-lat1) * Math.PI/180;
const Δλ = (lon2-lon1) * Math.PI/180;
const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c; // in miles
}
function getBearing(lat1, lon1, lat2, lon2) {
const φ1 = lat1 * Math.PI/180; // φ, λ in radians
const φ2 = lat2 * Math.PI/180;
const λ1 = lon1 * Math.PI/180;
const λ2 = lon2 * Math.PI/180;
const y = Math.sin(λ2-λ1) * Math.cos(φ2);
const x = Math.cos(φ1)*Math.sin(φ2) -
Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1);
const θ = Math.atan2(y, x);
return (θ*180/Math.PI + 360) % 360; // in degrees
}
function getCompassDirection(bearing) {
const directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
const index = Math.round(bearing / 45) % 8;
return directions[index];
}
function requestDeviceOrientationPermission() {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
window.addEventListener('deviceorientation', handleOrientation);
} else {
alert('Permission denied, this will not work correctly');
}
})
.catch(console.error);
} else {
// For browsers that don't require permission (e.g., Android or older iOS)
window.addEventListener('deviceorientation', handleOrientation);
}
}
function rotateElement(element, degrees) {
element.style.webkitTransform = 'rotate('+degrees+'deg)';
element.style.mozTransform = 'rotate('+degrees+'deg)';
element.style.msTransform = 'rotate('+degrees+'deg)';
element.style.oTransform = 'rotate('+degrees+'deg)';
element.style.transform = 'rotate('+degrees+'deg)';
}
function handleOrientation(event) {
if (!UPDATE_EVENTS) {
return;
}
let heading;
// On Safari (iOS), use webkitCompassHeading for absolute compass heading
if (event.webkitCompassHeading !== undefined) {
heading = event.webkitCompassHeading; // 0 is North
} else if (event.alpha !== null) {
heading = event.alpha; // This is relative and not true north
} else {
UPDATE_EVENTS = false;
alert('Orientation not supported');
}
// Make sure the compass direction points correctly
rotateElement(compass, -heading);
// Rotate the text so that it's facing up
targetSpan.style.top = -50 + (Math.abs(90 - (Math.abs(heading - targetDirection) % 180)) / 90) * 10 + 'px';
rotateElement(targetSpan, heading - targetDirection);
}
let targetDirection = null;
window.onload = () => {
compass = document.querySelector('#compass');
targetSpan = document.querySelector('#target span');
compass.addEventListener('click', (event) => {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
// Set the distance display
const distance = getDistance(position.coords.latitude, position.coords.longitude, TARGET.lat, TARGET.lon);
document.querySelector('#distance .value').innerHTML = Math.round(distance).toLocaleString() + ' miles';
// Get the direction of the target
targetDirection = getBearing(position.coords.latitude, position.coords.longitude, TARGET.lat, TARGET.lon);
// Set the display and move the indicator
document.querySelector('#direction .value').innerHTML = getCompassDirection(targetDirection);
rotateElement(document.querySelector('#target'), targetDirection);
// Handle the title text
document.querySelector('.plus').style.display = 'block';
targetSpan.innerHTML = '<?php echo $target; ?>';
targetSpan.style.position = 'absolute';
targetSpan.style.left = 'calc(50% - 21px)';
});
requestDeviceOrientationPermission();
} else {
UPDATE_EVENTS = false;
alert('Position not supported');
}
});
};
</script>
</head>
<body>
<div class="wrapper">
<div class="header">
<div id="distance">
<span class="title">Distance</span>
<span class="value">⊘</span>
</div>
<div id="direction">
<span class="title">Direction</span>
<span class="value">⊘</span>
</div>
</div>
<div class="hole">
<div class="pin">
<svg height="40" width="40">
<polygon points="18,0 22,0 22,7, 18,7" fill="rgb(217, 255, 110, 13%)"></polygon>
</svg>
</div>
<div id="compass">
<div id="n">N</div>
<div id="e">E</div>
<div id="s">S</div>
<div id="w">W</div>
<div id="target">
<svg height="40" width="40">
<polygon class="triangle" points="10,10 30,10 20,0" fill="#d9ff6e"></polygon>
</svg>
<span>Tap to find<br /><?php echo $descriptor; ?></span>
</div>
</div>
<div class="plus">+</div>
</div>
</div>
</body>
</html>