-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
58 lines (50 loc) · 1.5 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
56
57
58
const weather = document.querySelector(".js-weather");
const API_KEY = "1f51d81c6f08715fcd88f70f6d601bd2";
const COORDS = "coords";
function getWeather(lat, lon) {
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`
)
.then(function(response) {
return response.json();
})
.then(function(json) {
// console.log(json);
const temperature = json.main.temp;
const place = json.name;
weather.innerHTML = `${temperature} @ ${place}`;
}); //.then 은 데이터가 전부 들어온 이후 호출
}
function saveCoords(coordsObj) {
localStorage.setItem(COORDS, JSON.stringify(coordsObj));
}
function handleGeoSuccess(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const coordsObj = {
latitude,
longitude //Object의 변수와 key를 같게 하려면 : 없이 쓰면 됨
};
saveCoords(coordsObj);
getWeather(latitude, longitude);
}
function handleGeoError() {
console.log("Cannot access to geo location");
}
function askForCoords() {
navigator.geolocation.getCurrentPosition(handleGeoSuccess, handleGeoError);
}
function loadCoords() {
const loadedCoords = localStorage.getItem(COORDS);
if (loadedCoords === null) {
askForCoords();
} else {
const parsedCoords = JSON.parse(loadedCoords);
// console.log(parseCoords);
getWeather(parsedCoords.latitude, parsedCoords.longitude);
}
}
function init() {
loadCoords();
}
init();