-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
55 lines (49 loc) · 1.42 KB
/
weather.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
const weather = document.querySelector(".js-weather");
const API_KEY = "9f42a8c72c12611107970f3fa2ede3ef";
const COORDS = "coords";
function getWeather(lat, lng) {
fetch(
`http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_KEY}&units=metric`
)
.then(function (response) {
//fetch로 전달받은 데이터 인자로 들어감
return response.json(); // json으로 형식 변환
})
.then(function (json) {
const temperature = json.main.temp;
const place = json.name;
weather.innerHTML = `${temperature} @ ${place}`;
});
}
function saveCoords(coordsObj) {
localStorage.setItem(COORDS, JSON.stringify(coordsObj));
}
function handleGeoSucces(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const coordsObj = {
latitude,
longitude,
};
saveCoords(coordsObj);
getWeather(latitude, longitude);
}
function handleGeoError() {
console.log(position);
}
function askForCoords() {
navigator.geolocation.getCurrentPosition(handleGeoSucces, handleGeoError); //위치 정보 요청 (Weather API)
}
function loadCoords() {
const loadedCoords = localStorage.getItem(COORDS);
if (loadedCoords === null) {
askForCoords();
} else {
const parseCoords = JSON.parse(loadedCoords);
getWeather(parseCoords.latitude, parseCoords.longitude);
}
}
function init() {
loadCoords();
}
init();