Geolocation (날씨와 위치)

✨ 강은비·2022년 1월 7일
1

JS

목록 보기
18/19
post-thumbnail

📙 Geolocation API

사용자의 현재 위치를 가져오는 API
navigator.geolocation.getCurrentPosition(success, error, options);

  • success : GeolocationPosition 객체를 유일한 매개변수로 받는 콜백함수
  • error: GeolocationPositionError 객체를 유일한 매개변수로 받는 콜백함수
  • GeolocationPosition 인터페이스: 주어진 시간에 장치가 위치한 지점을 나타냄.
    • Position.coords: 주어진 시간의 위치
      • Position.coords.latitude / Position.coords.longitude
    • Position.timestamp: 위치를 기록한 시간
  • GeolocationPositionError: 에러 발생 이유를 나타냄
    • Error.code / Error.message

📙 Weather API

API call by geographic coordinates! (lat, lon, API_KEY)


📙 fetch 함수

원격 API를 간편하게 호출할 수 있도록 브라우저에서 제공하는 함수!

  • 첫 번째 인자로 접근하고자 하는 URL, 두 번째 인자로 옵션 객체를 받고 Promise 객체를 반환한다.
  • 반환된 객체는 API 호출이 성공했을 경우 응답 객체 (response)를 resolve하고, 실패했을 경우 예외 객체(error)를 reject한다.
fetch(url)
	.then(response => response.json())
    .then(data => console.log(data));
  • response.json(): 응답 객체로부터 JSON 포맷의 응답 전문을 자바스크립트 객체로 변환하여 얻을 수 있다.

0개의 댓글