From 30d18a3839eb7195e743bf88170f7c5e0b2eb6b8 Mon Sep 17 00:00:00 2001 From: Paul Jones Date: Fri, 13 Sep 2024 14:55:08 -0600 Subject: [PATCH] Clean up index-page.js, moving utility function to utilities.js --- scripts/index-page.js | 55 +++++++++++++++++++++++-------------------- scripts/utilities.js | 10 ++++++++ 2 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 scripts/utilities.js diff --git a/scripts/index-page.js b/scripts/index-page.js index 2ab5c96..55747a3 100644 --- a/scripts/index-page.js +++ b/scripts/index-page.js @@ -1,23 +1,22 @@ import { ISSApi } from "./iss-api.js"; +import { calculateAngle } from "./utilities.js"; let map; let marker; const issApiInstance = new ISSApi; -const getIssPositionApiCall = async () => { - try { - const issPosition = await issApiInstance.getIssPosition(); - return issPosition; - } catch (error) { - console.error(error) - } -} - let prevPosition = null; -const issPosititoData = async () => { +const issPosititoData = async (isInitialCall = false) => { try { const { latitude, longitude } = await issApiInstance.getIssPosition(); + const newLatLng = [latitude, longitude]; + + if (isInitialCall) { + // For the initial call, just store the position and return + prevPosition = newLatLng; + return; + } const issIcon = L.icon({ iconUrl: './assets/images/iss-icon.png', @@ -26,32 +25,36 @@ const issPosititoData = async () => { }); if (!map) { - map = L.map('map').setView([latitude, longitude], 3); + map = L.map('map').setView(newLatLng, 3); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {}).addTo(map); - marker = L.marker([latitude, longitude], {icon: issIcon, rotationAngle: 0, rotationOrigin: 'center center'}).addTo(map); - } else { - const newLatLng = [latitude, longitude]; - if (prevPosition) { - const angle = calculateAngle(prevPosition, newLatLng); - marker.setRotationAngle(angle); - } - map.setView(newLatLng, 3); + } + + if (!marker && prevPosition) { + const angle = calculateAngle(prevPosition, newLatLng); + marker = L.marker(newLatLng, {icon: issIcon, rotationAngle: angle, rotationOrigin: 'center center'}).addTo(map); + } else if (marker && prevPosition) { + const angle = calculateAngle(prevPosition, newLatLng); marker.setLatLng(newLatLng); + marker.setRotationAngle(angle); } - prevPosition = [latitude, longitude]; + + map.setView(newLatLng, 3); + prevPosition = newLatLng; } catch (error) { console.error("Error updating ISS position:", error); } } -function calculateAngle(prev, next) { - const dx = next[1] - prev[1]; - const dy = next[0] - prev[0]; - const angle = Math.atan2(dy, dx) * 180 / Math.PI; - return angle; +// Initial setup with two API calls +async function initializeISS() { + await issPosititoData(true); + // Wait for 1 second due to API rate limit + await new Promise(resolve => setTimeout(resolve, 1000)); + await issPosititoData(); } -await issPosititoData(); +// Start the process +initializeISS(); setInterval(issPosititoData, 10000); //auto-response diff --git a/scripts/utilities.js b/scripts/utilities.js new file mode 100644 index 0000000..9017cea --- /dev/null +++ b/scripts/utilities.js @@ -0,0 +1,10 @@ +export function calculateAngle(prev, next) { + const [lat1, lon1] = prev.map(x => x * Math.PI / 180); + const [lat2, lon2] = next.map(x => x * Math.PI / 180); + + const y = Math.sin(lon2 - lon1) * Math.cos(lat2); + const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); + const angle = Math.atan2(y, x) * 180 / Math.PI; + + return (angle + 360) % 360; +}