강의, 책/[JS] 바닐라 JS로 크롬 앱 만들기
#8. Weather
hye3193
2024. 2. 2. 14:34
Geolocation
function onGeoOk() {
// geolocation 정보를 받아오는 것을 성공했을 경우 실행
const lat = position.coords.latitude;
const lng = position.coords.longitude;
}
function onGeoError() {
// Error 발생 시 실행할 코드
}
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
API 받아오기
// 1. 해당하는 API KEY 발급
const API_KEY = "api_key";
// 2. 제공되는 url에 API_KEY를 비롯한 정보를 넣고 불러오기
const url = `url`;
fetch(url)
.then(response => reponse.json())
.then(data => {
const name = data.name;
const weather = data.weather[0].main
});
* fetch(url)을 사용하면 자바스크립트에서 해당 url 주소를 대신 불러준다