Skip to content

Commit d908cff

Browse files
committed
[JSMomentum] = Geo location api
1 parent 5bc2849 commit d908cff

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

JSforBeginners/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ <h4 class="js-greetings greetings"></h4>
2222
</form>
2323
<ul class="js-toDoList">
2424
</ul>
25+
<span class="js-weather"></span>
2526
<script src="clock.js"></script>
2627
<script src="gretting.js"></script>
2728
<script src="todo.js"></script>

JSforBeginners/weather.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
const weather = document.querySelector(".js-weather");
2+
13
const API_KEY = 'cfb3b9813c0b7484f3e348497ab68b28';
24
const COORDS = 'coords';
35

6+
function getWeather(lat, lon) {
7+
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`
8+
).then(function(response) {
9+
return response.json();
10+
}).then(function(json) {
11+
const temperature = json.main.temp;
12+
const place = json.name;
13+
14+
weather.innerText = `${temperature} @ ${place}`;
15+
});
16+
}
17+
418
function saveCoords(coordsObj) {
519
localStorage.setItem(COORDS, JSON.stringify(coordsObj));
620
}
@@ -13,6 +27,7 @@ function handleGeoSuccess(position) {
1327
longitude,
1428
};
1529
saveCoords(coordsObj);
30+
getWeather(latitude, longitude);
1631
}
1732

1833
function handleGeoError() {
@@ -28,9 +43,11 @@ function loadCoords() {
2843
const loadedCoords = localStorage.getItem(COORDS);
2944

3045
if (loadedCoords === null) {
31-
askForCoords();
46+
askForCoords();
3247
} else {
33-
48+
const parseCoords = JSON.parse(loadedCoords);
49+
getWeather(parseCoords.latitude, parseCoords.longitude);
50+
console.log(parseCoords);
3451
}
3552
}
3653

0 commit comments

Comments
 (0)