[TIL] 2023/10/19 JS 현재 위치/날씨 가져오기

김민재·2023년 10월 19일
0

TIL

목록 보기
21/172

navigator.geolocation.getCurrentPosition(function (pos) {
console.log(pos);
var latitude = pos.coords.latitude;
var longitude = pos.coords.longitude;
console.log("현재 위치는 : " + latitude + ", " + longitude);
});
// 현재 위치 가져오기

navigator.geolocation.getCurrentPosition(function (pos) {
console.log(pos);
var latitude = pos.coords.latitude;
var longitude = pos.coords.longitude;
let url = https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=658d847ef1d28e72e047ab0c5a476d54&units=metric;
// url에 데이터를 가져올 때까지 기다려 주세요
fetch(url)
.then((res) => res.json())
.then((data) => {
console.log(data);
const header = document.querySelector(".header");
const weatherDiv = document.createElement("div");
weatherDiv.classList.add("weather");
weatherDiv.innerText = 현재 날씨: ${data.main.temp}도 ${data.weather[0].main};
header.append(weatherDiv);
});
// innerHTML을 사용해서 바꿔주자
});

profile
개발 경험치 쌓는 곳

0개의 댓글

관련 채용 정보