-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.js
More file actions
62 lines (53 loc) · 1.99 KB
/
weather.js
File metadata and controls
62 lines (53 loc) · 1.99 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
61
62
const weather = document.querySelector(".js-weather");
const COORDS = 'coords';
const API_KEY = 'b08453aefb62a2d40d599291d131b361';
function getWeather(lat, lng){
console.log('lat: ', lat, ' , lng: ', lng, ' , url: ', `https://cors-anywhere.herokuapp.com/https://samples.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_KEY}`);
fetch(`https://cors-anywhere.herokuapp.com/https://samples.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_KEY}`,
).then(function(response){
console.log(response);
return response.json();
}).then(function(json){
console.log('json: ', json);
console.log('place: ', json.name, ' , temp: ', json.main.temp);
// 위도 경도는 잘 가져오는데 api 서버의 응답값 이상함. (왜 일본 날씨값을 가져오는지...)
// const temperature = (Number(json.main.temp)-32) / 1.8;
// const place = json.name;
const temperature = 20;
const place = '대전시';
weather.innerText = `${place} : ${temperature}°C`;
});
}
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, // == latitude: latitude
longitude
};
console.log(position, coordsObj);
saveCoords(coordsObj);
getWeather(latitude, longitude);
}
function handleGeoError(){
console.log('지역에 접근할 수 없습니다!')
}
function askForCoords(){
navigator.geolocation.getCurrentPosition(handleGeoSucces, handleGeoError);
}
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();