프론트/React Native, React

[리액트네이티브] react-native 날씨 가져오기(Expo)

연지양갱 2023. 6. 27. 09:36
728x90
반응형
SMALL

앱에서 만드는 부분은 산책이라

날씨 api를 넣어야겠다고 생각함

그래서

날씨 api를 쓰려고 한다

https://velog.io/@yhko1992/%EB%82%A0%EC%94%A8-%EC%A0%95%EB%B3%B4-%EB%B0%9B%EC%95%84%EC%98%A4%EA%B8%B0

 

날씨 정보 받아오기

우선 위도, 경도를 navigator 함수를 이용해 받아온다. 그리고 그 위치로 장소를 파악해 해당 장소의 날씨데이터를 전송해준다.weather.js 파일을 만들고 언제나 그렇듯 html 에 추가한다.navigator{:class="

velog.io

현재위치 가져오는 건

이미 모두 구현되어 있어서

위도경도에 따라 날씨 정보를 가져오면 됐다

openweather에서 api키를 발급받아야함

  1. 회원가입

https://openweathermap.org/api

 

Weather API - OpenWeatherMap

Please, sign up to use our fast and easy-to-work weather APIs. As a start to use OpenWeather products, we recommend our One Call API 3.0. For more functionality, please consider our products, which are included in professional collections.

openweathermap.org

2. api key받아오기

내 닉네임, 즉 마이페이지 부분을 클릭하면

Myservices가 있다

API keys를 클릭하여 키를 확인한다

3. 메인페이지에서 상단 메뉴바에서 API를 클릭

4. One Call API에서 API doc을 클릭

5. API call 하는 방법을 알 수 있음

https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}

이 코드를 사용하여 날씨를 가져올 수 있다

https://nomadcoders.co/react-native-for-beginners/lectures/3127

 

All Courses – 노마드 코더 Nomad Coders

초급부터 고급까지! 니꼬쌤과 함께 풀스택으로 성장하세요!

nomadcoders.co

 

여기도 참고하면서 코드를 짰지만

오류가 났다

그래서 메일을 찾아보니까

3.0은 무료 사용자는 사용할 수 없어서

2.5로 해야하고

weather?을 해야했다

아무튼 이렇게 해서 값을 가져왔다...

 

let location = await Location.getCurrentPositionAsync({});
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${location.coords.latitude.toFixed(5)}&lon=${location.coords.longitude.toFixed(5)}&appid=${API_KEY}&units=metric`);
const res = await response.json()
console.log(res)

 

 

반응형