Skip to content

Commit

Permalink
Clean up index-page.js, moving utility function to utilities.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljones0 committed Sep 13, 2024
1 parent a937785 commit 30d18a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
55 changes: 29 additions & 26 deletions scripts/index-page.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions scripts/utilities.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 30d18a3

Please sign in to comment.