-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (57 loc) · 2.26 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
'use strict';
const countriesContainer = document.querySelector('.countries');
const renderCountry = function (data, className = '') {
const html = `
<article class="country ${className}">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)}</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${
data.currencies[0].name
}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
};
const renderError = function (msg) {
countriesContainer.insertAdjacentHTML('beforeend', msg);
countriesContainer.style.opacity = 1;
};
// creating a function to get the current location of the user
const getPosition = function () {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
position => resolve(position),
err => reject(err)
);
}); //returning the result of the current position as promise
};
//creating the actuall whereamI function which will take the current location from the getLocation function
const whereamI = async function () {
try {
const pos = await getPosition();
const { latitude: lat, longitude: lng } = pos.coords;
const resGeo = await fetch(
`https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lng}&apiKey=355eb6d0b0bb48c595f20198c7671df0`
);
if (!resGeo.ok) throw new Error('Problem getting location data');
const dataGeo = await resGeo.json();
const takeOutCountry = dataGeo.features[0].properties;
const res = await fetch(
`https://restcountries.com/v2/name/${takeOutCountry.country}`
);
const data = await res.json();
const index = takeOutCountry.country === 'India' ? 1 : 0;
renderCountry(data[index]);
} catch (err) {
renderError(`Something went wrong ${err.message} 🔥`);
}
};
whereamI();