-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (95 loc) · 3.54 KB
/
script.js
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
const worldImg = document.getElementById('worldMap');
const iss = document.getElementById('iss');
const title = document.getElementById('title');
const btnContainer = document.getElementById('btnContainer')
const latLegend = document.getElementById('latLegend');
const longLegend = document.getElementById('longLegend');
const newQuoteBtn = document.getElementById('new-quote');
let loc = {};
async function componentDidMount() {
/* const apiUrl = 'http://api.open-notify.org/iss-now.json'; */
const apiUrl = "https://api.wheretheiss.at/v1/satellites/25544";
try {
const response = await fetch (apiUrl);
loc = await response.json();
}catch(error) {
alert(error);
}
mapCenter();
}
async function afterClick () {
/* const apiUrl = 'http://api.open-notify.org/iss-now.json'; */
const apiUrl = "https://api.wheretheiss.at/v1/satellites/25544";
try {
const response = await fetch (apiUrl);
loc = await response.json();
}catch(error) {
alert(error);
}
mapCenter();
}
function mapCenter () {
/* CENTERING THE MAP IMAGE AND BUILDING THE SCREEN */
/* Variables to define the map size*/
let mapHeight = 0;
let mapWidth = 0;
/*Variables to calculate the latitude and longitude of the ISS*/
/* let lat = loc.iss_position.latitude;
let long = loc.iss_position.longitude; */
let lat = loc.latitude.toPrecision(5);
let long = loc.longitude.toPrecision(5);
/* Variables for the map position on the page*/
let topOffset = 0;
let leftOffset = 0;
/* Variables for setting up the iss current position*/
let mapHeightUnit = 0;
let mapWidthUnit = 0;
let mapCenterHeight = 0;
let mapCenterWidth = 0;
let issTop = 0;
let issLeft = 0;
iss.style.top = 0;
iss.style.left = 0;
/* Verifying the screen apect ratio*/
let windowHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
let windowWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
let screenAspectRatio = windowHeight/windowWidth;
/* Setting up the size of the map*/
if (screenAspectRatio < 1) {
mapHeight = windowWidth * 30/100;
mapWidth = windowWidth * 60/100;
}else {
mapHeight = windowWidth * 40/100;
mapWidth = windowWidth * 80/100;
}
worldImg.style.width = Math.round(mapWidth) + 'px';
worldImg.style.height = Math.round(mapHeight) + 'px';
/* Setting the map position on the page*/
topOffset = Math.round((windowHeight - mapHeight)/2);
leftOffset = Math.round((windowWidth - mapWidth)/2);
worldImg.style.top = topOffset + 'px';
worldImg.style.left = leftOffset + 'px';
/*Setting up the iss current position*/
mapHeightUnit = mapHeight/180;
mapWidthUnit = mapWidth/360;
mapCenterHeight = topOffset + mapHeight/2;
mapCenterWidth = leftOffset + mapWidth/2;
issTop = mapCenterHeight - (lat * mapHeightUnit);
issLeft = mapCenterWidth + (long * mapWidthUnit);
iss.style.top = issTop + 'px';
iss.style.left = issLeft + 'px';
/*Styling the title*/
/* title.style.resize = 'both'; */
title.style.width = Math.round(mapWidth) + 'px';
title.style.top = (topOffset-100) + 'px';
title.style.left = leftOffset + 'px';
btnContainer.style.top = (topOffset+mapHeight) + 'px';
btnContainer.style.left = leftOffset + 'px';
newQuoteBtn.style.width = Math.round(mapWidth) + 'px';
/* latLegend.style.top = (topOffset-10) + 'px';
latLegend.style.left = leftOffset + 'px'; */
latLegend.textContent = `Latitude ${lat}`;
longLegend.textContent = `Longitude ${long}`;
}
newQuoteBtn.addEventListener('click', afterClick);
componentDidMount();