-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
222 lines (184 loc) · 5.83 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
<?php
$startTime = microtime(true);
$error = false;
$mapShareUrl = $kmlFeedUrl = '';
if (isset($_GET['q'])) {
if (isValidUrl($_GET['q'])) {
$kmlFeedUrl = $_GET['q'];
$id = getMapId($kmlFeedUrl);
if (empty($id)) {
$error = true;
} else {
$mapShareUrl = 'https://share.garmin.com/' . $id;
}
} else {
$kmlFeedUrl = "https://inreach.garmin.com/feed/share/" . $_GET['q'];
$mapShareUrl = 'https://share.garmin.com/' . $_GET['q'];;
}
} else {
// exmaple URLs
$mapShareUrl = 'https://share.garmin.com/DJFOX';
$kmlFeedUrl = 'https://inreach.garmin.com/feed/share/DJFOX';
}
$coordinatesString = $lastTimestamp = '';
$kmh = $mph = 0;
if (!isValidUrl($kmlFeedUrl)) {
$error = true;
} else {
// Load the KML file
try {
$kml = simplexml_load_file($kmlFeedUrl);
if (empty($kml)) {
$error = true;
} else {
// Register the KML namespace (required for proper XML handling)
$kml->registerXPathNamespace('kml', 'http://www.opengis.net/kml/2.2');
}
} catch (Exception $e) {
$error = true;
}
if (!$error) {
// Iterate over all placemarks
foreach ($kml->xpath('//kml:Placemark') as $placemark) {
// Get the name of the placemark
if (isset($placemark->name)) {
$name = (string) $placemark->name;
}
// echo "Placemark Name: $name\n<br/>";
// echo "<br>";
// Accessing custom attributes (ExtendedData)
if ($placemark->ExtendedData) {
foreach ($placemark->ExtendedData->Data as $data) {
$attrName = (string) $data['name']; // attribute name
$attrValue = (string) $data->value; // attribute value
// echo "$attrName: $attrValue<br>";
if ($attrName == 'Time') {
$lastTimestamp = $attrValue;
}
if ($attrName == 'Latitude') {
$coordinatesString .= "$attrValue, ";
}
if ($attrName == 'Longitude') {
$coordinatesString .= "$attrValue";
}
if ($attrName == 'Velocity') {
if (strpos($attrValue, 'km/h') !== false) {
$mph = kmhToMph($attrValue);
} else {
$mph = $attrValue;
}
}
}
}
// echo "<br/><br/>";
}
$dest = urlencode($coordinatesString);
$google = "https://www.google.com/maps/dir/?api=1&destination=$dest&travelmode=walking";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TrailView - KML</title>
</head>
<body>
<?php if ($error): ?>
<strong>An error has occurred.</strong>
<?php else: ?>
<hr />
<strong>Garmin Data for: '<?php echo $name; ?>'</strong>
<br>
<strong>Last update <?php echo $lastTimestamp ?></strong>
<br>
<strong>speed: <?php echo $mph; ?> mph</strong>
<br>
<input type="text" id="textToCopy" value="<?php echo $coordinatesString ?>" />
<button onclick="copyToClipboard()">Copy Coordinates to Clipboard</button>
<br><br>
<button onclick="refreshPage()">Refresh Page</button>
<br><br><br>
<a href="<?php echo $google ?>">open google maps from your location to dj</a>
<br><br><br>
<a href="<?php echo $mapShareUrl; ?>" target="_blank">garmin map share</a>
<script>
function refreshPage() {
location.reload();
}
function copyToClipboard() {
const text = document.getElementById('textToCopy').value;
if (navigator.clipboard && window.isSecureContext) {
// Use the Clipboard API
navigator.clipboard.writeText(text);
} else {
// Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.log('Failed to copy text: ', err);
}
}
document.body.removeChild(textArea);
}
</script>
<?php endif; ?>
<br><br><br>
<footer>
<?php
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
echo "Page loaded in $executionTime seconds.";
?>
</footer>
</body>
</html>
<?php
/**
* Check is a url is valid
*
* @param string $kmlFeedUrl
* @return boolean
*/
function isValidUrl($kmlFeedUrl)
{
return filter_var($kmlFeedUrl, FILTER_VALIDATE_URL) !== false;
}
/**
* Convert km/h to mi/h
*
* @param int|float $kmh
* @return int|float
*/
function kmhToMph($kmh)
{
$kmh = (float) str_replace('km/h', '', $kmh);
$conversionFactor = 0.621371;
$mph = $kmh * $conversionFactor;
return $mph;
}
/**
* Get user id from url
*
* @param string $s
* @return string
*/
function getMapId($s)
{
$r = explode('/', $s);
$l = end($r);
if (empty($l)) {
$l = prev($r);
}
return $l;
}