Files
StuTracker/index.html
2024-06-14 16:00:05 -04:00

160 lines
6.3 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Stutracker</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/* Ensure the map container takes up the full page */
#map {
height: 100%;
width: 100%;
position: absolute;
top: 35px;
bottom: 0;
left: 0;
right: 0;
}
/* Style for the banner */
#banner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 35px;
background-color: #333;
color: #fff;
text-align: center;
line-height: 30px; /* Adjust as needed */
font-size: 24px; /* Adjust as needed */
}
</style>
<!-- Include Leaflet CSS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<!-- Include Leaflet JavaScript -->
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<!-- Include Leaflet GPX plugin -->
<script src="https://unpkg.com/leaflet-gpx/gpx.js"></script>
<!-- Include Leaflet Omnivore plugin -->
<script src="https://unpkg.com/leaflet-omnivore@0.3.4/leaflet-omnivore.min.js"></script>
</head>
<body>
<div id="banner">StuTracker</div>
<div id="map"></div>
<script>
// Initialize the map centered on the specified coordinates and zoom level
var map = L.map('map').setView([40.9734, -73.9256], 11);
// Add OpenStreetMap tiles
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// KML URL with username and password
var kmlUrl = 'NDB92';
// var kmlUrl = 'https://USERNAME:abodyinmotion@share.garmin.com/Feed/Share/NDB92';
//var kmlUrl = 'https://share.garmin.com/Feed/Share/NDB92';
// Use CorsProxy.io to handle CORS for the KML file
//var corsProxy = 'https://corsproxy.io/?';
//var proxyKmlUrl = corsProxy + encodeURIComponent(kmlUrl);
// Load the KML file
//omnivore.kml(kmlUrl).addTo(map);
// Load the KML file and customize the marker icon
// var kmlLayer = omnivore.kml(kmlUrl, null, L.geoJSON(null, {
// pointToLayer: function (feature, latlng) {
// return L.marker(latlng, {
// icon: L.icon({
// iconUrl: 'images/marker-icon-2x.png',
// iconSize: [25, 41], // size of the icon
// iconAnchor: [12, 41], // point of the icon which will correspond to marker's location
// popupAnchor: [1, -34], // point from which the popup should open relative to the iconAnchor
// shadowSize: [41, 41] // size of the shadow
// })
// });
// }
// })).addTo(map);
// Function to calculate time difference in minutes
function getTimeDifference(updatedTime) {
var now = new Date();
var updateTime = new Date(updatedTime);
var difference = now.getTime() - updateTime.getTime();
return Math.round(difference / 60000); // Convert milliseconds to minutes
}
// Fetch KML file metadata to get the update time
fetch(kmlUrl)
.then(response => response.text())
.then(data => {
var parser = new DOMParser();
var kmlDoc = parser.parseFromString(data, 'application/xml');
// Try to find the update time
var updateTime;
var timeStamp = kmlDoc.querySelector('TimeStamp > when');
if (timeStamp) {
updateTime = timeStamp.textContent;
} else {
var updated = kmlDoc.querySelector('updated');
if (updated) {
updateTime = updated.textContent;
}
}
if (updateTime) {
var timeDifference = getTimeDifference(updateTime);
document.getElementById('banner').innerText = `StuTracker - Location Last updated: ${timeDifference} minutes ago`;
} else {
document.getElementById('banner').innerText = 'StuTracker - Update time not found';
}
var kmlLayer = omnivore.kml(kmlUrl, null, L.geoJSON(null, {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon: L.icon({
iconUrl: 'images/marker-icon-2x.png',
iconSize: [25, 41], // size of the icon
iconAnchor: [12, 41], // point of the icon which will correspond to marker's location
popupAnchor: [1, -34], // point from which the popup should open relative to the iconAnchor
shadowSize: [41, 41] // size of the shadow
})
});
}
})).addTo(map);
// Adjust the map to fit the KML layer
// var bounds = kmlLayer.getBounds();
// map.fitBounds(bounds);
})
.catch(error => console.error('Error fetching KML:', error));
// GPX URL - Change 'croton.gpx' to the filename of your GPX file
var gpxUrl = 'GAP_C_O_-_Day_1 (merged).gpx';
// Load the GPX file
new L.GPX(gpxUrl, {
async: true,
marker_options: {
startIconUrl: 'images/marker-icon.png',
endIconUrl: 'images/marker-icon.png',
shadowUrl: 'images/marker-shadow.png'
}
}).on('loaded', function(e) {
map.fitBounds(e.target.getBounds());
}).addTo(map);
// Optionally, handle any errors
new L.GPX(gpxUrl).on('error', function(error) {
console.error('Failed to load GPX:', error);
});
</script>
</body>
</html>