Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- codability
- expensive operation
- NSManagedObject SubClass
- CoreData Filter
- LightWeight Migration
- Swift
- 일급 객체
- NSPredicates
- Associated Value
- iOS Static Library
- NSSortDescriptor
- Clean swift
- 다익스트라 이해
- iOS Static Library 사용하는방법
- 트레일링 클로저
- CoreData
- leetcode #01
- CoreData Stack
- 1009번
- Java
- CoreData Concurrency
- persistentStoreCoordinator
- Swift closure
- 스위프트 클로저
- Raw value and Associated value
- Swift LinkedList
- Swift 고차함수
- Persistent store Coordinator
- dateFormatter
- 2022 부스트캠프
Archives
- Today
- Total
하루를살자
JS Clone Project [Weather] - 1, weather API 본문
Key Points
- use of open API.
- Weather API from https://openweathermap.org/
- Use of API URL
//Be sure to make the API_KEY as string
const API_KEY ="cc9adc5ce92a3b5e53924dedbc9ca38a";
//if getCurrentPosition is succeed , it calls onGeoOk function with geolocation object information
function onGeoOk(pos){
const lat = pos.coords.latitude;
const long = pos.coords.longitude;
console.log(lat,long);
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${API_KEY}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data =>{
const city= document.querySelector("#weather span:first-child");
const weather = document.querySelector("#weather span:nth-child(2)");
const temp = document.querySelector("#weather span:nth-child(3)");
city.innerHTML = data.name;
weather.innerHTML = data.weather[0].main;
temp.innerHTML = `${data.main.temp} Celcius`;
});
}
function onGeoError(){
alert("Can't find you.!");
}
//getCurrentPosition requires 2 arugments, 1. successfull callback 2. fail callback
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
Staff learned
- use fetch(URL) to get a response to that URL.
--> fetch returns Promise which means something is going to happen after once the page get response from the fetch.
--> fetch(URL).then() : refers that do something once the fetch is completed. In this case,
"fetch(URL).then(response=> response.json()).then(data =>{
console.log(data.weather[0].main);
}) "
response.json() ==> get the response as JSON
data =>{} : make use of the recieved data.
'JS' 카테고리의 다른 글
JS Clone Project [Weather] - 1, getting location (0) | 2021.12.09 |
---|---|
JS Clone Project [to-do list] - 4, Deleting to-Do list (0) | 2021.12.09 |
JS Clone Project [to-do list] - 3, Saving to-do list (0) | 2021.12.09 |
JS Clone Project [to-do list] - 1,2 (0) | 2021.12.05 |
JS Clone Project [Background] (0) | 2021.12.05 |
Comments