-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
77 lines (71 loc) · 2.88 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<meta name="description" content="Using NASA's Earth API to display recent satellite imagery of your current city">
<meta name="keywords" content="NASA, Earth, View, JavaScript">
<meta name="author" content="Connor Zacharias">
<title>What Does it Look Like Where I Am?</title>
<style>
p, h1 {
color: green;
}
* {
margin: 0;
padding: 0;
}
.imgbox {
display: grid;
height: 100%;
}
.center-fit {
max-width: 100%;
max-height: 100vh;
margin: auto;
}
</style>
</head>
<body>
<center>
<p id="loading placeholder">loading...</p>
<div class="imgbox">
<img id="city-view" class="center-fit" src=''>
</div>
</center>
<script>
document.addEventListener("DOMContentLoaded", function() {
fetch("https://api.ipify.org?format=json")
.then(response => response.json())
.then(data => {
const ip = data.ip;
fetch(`https://api.geoapify.com/v1/ipinfo?ip=${ip}&apiKey=afe3e651ae3d458e88aeccb8f34f77dc`)
.then(res => res.json())
.then(data => {
const name = data.city.name;
fetch(`https://api.openweathermap.org/geo/1.0/direct?q=${name}&limit=1&appid=ed5facf7209c0bd5b2317f406a6f9560`)
.then(res => res.json())
.then(data => {
return data[0];
})
.then(coords => {
var year = new Date().getFullYear();
const imageUrl = `https://api.nasa.gov/planetary/earth/imagery?lon=${coords.lon}&lat=${coords.lat}&date=${year}-01-01&dim=0.15&api_key=jeGQgs4QLyLWXs3UN8RjIUbTd9pWJtgAymdzLnnX`;
fetch(imageUrl)
.then(res => res.blob())
.then(data => {
const imageObjectURL = URL.createObjectURL(data);
document.getElementById("loading placeholder").remove();
document.getElementById("city-view").src = imageObjectURL;
});
});
});
})
.catch(error => {
console.error("Error fetching IP address:", error);
});
});
</script>
</body>
</html>