-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (50 loc) · 1.96 KB
/
app.js
File metadata and controls
60 lines (50 loc) · 1.96 KB
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
// Variables
const btn = document.querySelector("#locationBtn");
const img = document.querySelector(".img");
const h1 = document.querySelector(".h1");
const p = document.querySelector(".p");
// Functions
const relaodPage = () => {
btn.addEventListener("click", () => {
window.location.reload();
});
}
const countryDetails = (country) => {
fetch(`https://restcountries.com/v3.1/name/${country}`).then(res => res.json()).then(data => {
console.log(data);
img.src = data[0].flags.svg;
h1.style.marginTop = "15px";
h1.innerText = data[0].name.common;
p.classList.add("text-start", "pDetails");
p.innerHTML = `
Capital: ${data[0].capital[0]} <br>
Region: ${data[0].region} <br>
Population: ${data[0].population} <br>
Area: ${data[0].area} km²
`;
btn.innerHTML = "Get My Location Again";
relaodPage();
});
}
// Method 1
// const userLocation = () => {
// // IP Address API
// let response = fetch(`https://api.ipify.org/?format=json`).then(res => res.json()).then(ipData => console.log(ipData));
// // Geolocation API
// let response1 = fetch(`https://geo.ipify.org/api/v2/country,city?apiKey=at_xG2vwzjJvFweMKK2Kmh57tUlYROrN&ipAddress=${ipData.ip}`).then(res1 => res1.json()).then(data1 => console.log(data1));
// }
// Method 2
const userLocation = async () => {
// IP Address API
let response = await fetch(`https://api.ipify.org/?format=json`)
let ipData = await response.json();
console.log(ipData);
// Geolocation API
let response1 = await fetch(`https://geo.ipify.org/api/v2/country,city?apiKey=at_xG2vwzjJvFweMKK2Kmh57tUlYROrN&ipAddress=${ipData.ip}`);
let data1 = await response1.json();
console.log(data1);
const {location : {country}} = data1;
console.log(country);
countryDetails(country);
}
btn.addEventListener("click", userLocation)