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
- 배포
- github
- 디지털하나로
- 미래내일일경험
- 맥북백틱
- 백틱
- 스나이퍼팩토리
- 디지털취업
- 개발자교육과정
- kdt
- 디지털하나로입학식
- 하나은행
- Next.js
- 네이버로그인창만들기
- 프로젝트캠프
- 프론트엔드배포
- 웅진씽크빅
- 취준생
- DIGITALHANARO
- `
- 프론트엔드개발자양성과정
- 깃허브 레포지토리와 로컬 코드 연결하기
- 부트캠프
- 버전생성프로세스
- 유데미
- udemy
- s3
- 맥북백틱입력
- 디지털교육
Archives
- Today
- Total
Land of Joe
[JavaScript] navigator.geolocation.getCurrentPosition(success함수,error함수) 본문
🌐 Web/🟡 JavaScript
[JavaScript] navigator.geolocation.getCurrentPosition(success함수,error함수)
Arendt 2023. 5. 15. 22:36Step1. 현재 위치의 위도, 경도 숫자 얻기
function onGeoOk(position){
const lat = position.coords.latitude;
const lng = position.coords.longitude;
console.log("You live in ", lat, lng);
}
function onGeoError(){
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoOk,onGeoError);
Step2. 위도, 경도 숫자를 장소로 바꿔준다
https://openweathermap.org/current
Current weather data - OpenWeatherMap
Access current weather data for any location on Earth including over 200,000 cities! We collect and process weather data from different sources such as global and local weather models, satellites, radars and a vast network of weather stations. Data is avai
openweathermap.org
openAPI를 활용
https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}
{lat}부분에 위 과정을 통해 얻은 위도(latitude) 값을 넣어주고
{lon}부분에 경도(longitude) 값을 넣어주고
{API key} 부분에 나의 개인 API key 값을 넣어주면 해당 위치의 날씨 정보가 나온다
javascript가 url을 직접 불러오도록 하는 것: fetch(url)
const API_KEY = '';
function onGeoOk(position){
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
fetch(url).then(response => response.json()).then(data=> {
const weather = document.querySelector('#weather span:first-child');
const city = document.querySelector('#weather span:last-child');
city.innerText = data.name;
weather.innerText = `${data.weather[0].main} / ${data.main.temp}'c`;
});
}
function onGeoError(){
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoOk,onGeoError);
개인 API key 값을 넣어주고 돌리면
'🌐 Web > 🟡 JavaScript' 카테고리의 다른 글
[JavaScript] 투두리스트 만들기() (0) | 2023.05.10 |
---|---|
[JavaScript] 명언 랜덤하게 보여주기(Math.random()) (0) | 2023.05.09 |
[JavaScript] 시계 만들기(setInterval, Date, padStart) (0) | 2023.05.08 |
[JavaScript] setInterval, setTimeout 공부(2) (0) | 2023.05.08 |
[JavaScript] localStorage API (0) | 2023.05.07 |