728x90
1. ๊ธฐ๋ฅ ๋ช ์ธ์
//1. ์ฑ์ด ์คํ๋์๋ง์ ํ์ฌ ์์น ๊ธฐ๋ฐ์ ๋ ์จ๊ฐ ๋ณด์ธ๋ค.
//2. ๋ ์จ ์ ๋ณด์๋ ๋์, ์ญ์จ, ํ์จ ๋ ์จ ์ํ
//3. 5๊ฐ์ ๋ฒํผ์ด ์๋ค (1๊ฐ๋ ํ์ฌ ์์น, 4๊ฐ๋ ๋ค๋ฅธ ๋์)
//4. ๋ก๋ฉ ์คํผ๋ ๊ธฐ๋ฅ ์ถ๊ฐ
2. weather api
https://openweathermap.org/api
๋งํฌ์์ ํ์๊ฐ์ ํ๊ณ ์ ๊ณต๋ฐ์ key๋ฅผ ์ด์ฉํ๊ณ
๋ฌด๋ฃ api๋ฅผ ์ฌ์ฉํด์ ์์น, ๋์ ๊ธฐ๋ฐ ๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์๋ณด๋๋ก ํ๋ค.
9007b03099d0209ad491e058a2d63c26
3. Using HTML Geolocation
https://www.w3schools.com/html/html5_geolocation.asp
ํด๋น ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ฌ ์์น๋ฅผ ๋ณด์ฌ์ฃผ๋ ์ฝ๋๋ฅผ App.js์ ์์ฑํ๋ค.
import './App.css';
import { useEffect, useState } from 'react';
//1. ์ฑ์ด ์คํ๋์๋ง์ ํ์ฌ ์์น ๊ธฐ๋ฐ์ ๋ ์จ๊ฐ ๋ณด์ธ๋ค.
//2. ๋ ์จ ์ ๋ณด์๋ ๋์, ์ญ์จ, ํ์จ ๋ ์จ ์ํ
//3. 5๊ฐ์ ๋ฒํผ์ด ์๋ค (1๊ฐ๋ ํ์ฌ ์์น, 4๊ฐ๋ ๋ค๋ฅธ ๋์)
//4. ๋ก๋ฉ ์คํผ๋ ๊ธฐ๋ฅ ์ถ๊ฐ
function App() {
const getCurrentLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
alert('ํ์ฌ ์์น๋ ' + latitude + ' ' + longitude);
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
useEffect(() => {
getCurrentLocation()
}, []);
return (
<div>
๋์ ๋ ์จ๋
</div>
);
}
export default App;
4. api๋ฅผ ํธ์ถํด์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ ์ธํ ํ๊ธฐ
import './App.css';
import { useEffect, useState } from 'react';
//1. ์ฑ์ด ์คํ๋์๋ง์ ํ์ฌ ์์น ๊ธฐ๋ฐ์ ๋ ์จ๊ฐ ๋ณด์ธ๋ค.
//2. ๋ ์จ ์ ๋ณด์๋ ๋์, ์ญ์จ, ํ์จ ๋ ์จ ์ํ
//3. 5๊ฐ์ ๋ฒํผ์ด ์๋ค (1๊ฐ๋ ํ์ฌ ์์น, 4๊ฐ๋ ๋ค๋ฅธ ๋์)
//4. ๋ก๋ฉ ์คํผ๋ ๊ธฐ๋ฅ ์ถ๊ฐ
const choice = {
Seoul: { name: 'Seoul' },
NewYork: { name: 'NewYork' },
Toronto: { name: 'Toronto' },
Paris: { name: 'Paris' },
Rondon: { name: 'Rondon' }
}
function App() {
const [userLocation, setUserLocation] = useState(null);
const clickCity = (userChoice) => {
setUserLocation(choice[userChoice]);
}
const getCurrentLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
getWeatherByCurrentLocation(latitude, longitude);
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
//await ์ฐ๊ณ ์ ํ๋ ํจ์๋ async ์ฌ์ผ ํ๋ค!
const getWeatherByCurrentLocation = async (lat, lon) => {
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=9007b03099d0209ad491e058a2d63c26`;
let response = await fetch(url) //๋น๋๊ธฐ(์ง๋
: url์ patchํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ!) : url ๋ก๋ฉํ ๋ค ์๋ต๊ฐ์ ๋ฐ๊ธฐ๋ก ํจ
let data = await response.json(); //์๋ต์์ json ์ถ์ถํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ
}
useEffect(() => {
getCurrentLocation()
}, []);
return (
<div>
<div>
<button onClick={() => clickCity('Seoul')}>Seoul</button>
</div>
<div>
</div>
</div>
);
}
export default App;
5. ๊ธฐ๋ณธ UI ์์
https://react-bootstrap.github.io/
์๋ฒ ์ข ๋ฃ ํ ์ค์น
npm install react-bootstrap bootstrap
๊ธฐ๋ณธ์ ์ธ ๋ถํธ์คํธ๋ฉ ๊น์ง ์ฒ๋ฆฌํด์ฃผ๊ณ ๋ ๋๋ง๋ ํด๋ผ ํ๋ฉด
์ง๊ธ๊น์ง์ ์์ค ์คํ
App.js
import './App.css';
import { useEffect, useState } from 'react';
import WeatherBox from "./component/WaetherBox";
import WeatherButton from "./component/WeatherButton";
import 'bootstrap/dist/css/bootstrap.min.css';
//1. ์ฑ์ด ์คํ๋์๋ง์ ํ์ฌ ์์น ๊ธฐ๋ฐ์ ๋ ์จ๊ฐ ๋ณด์ธ๋ค.
//2. ๋ ์จ ์ ๋ณด์๋ ๋์, ์ญ์จ, ํ์จ ๋ ์จ ์ํ
//3. 5๊ฐ์ ๋ฒํผ์ด ์๋ค (1๊ฐ๋ ํ์ฌ ์์น, 4๊ฐ๋ ๋ค๋ฅธ ๋์)
//4. ๋ก๋ฉ ์คํผ๋ ๊ธฐ๋ฅ ์ถ๊ฐ
const choice = {
Seoul: { name: 'Seoul' },
NewYork: { name: 'NewYork' },
Toronto: { name: 'Toronto' },
Paris: { name: 'Paris' },
Rondon: { name: 'Rondon' }
}
function App() {
const [userLocation, setUserLocation] = useState(null);
const clickCity = (userChoice) => {
setUserLocation(choice[userChoice]);
}
const getCurrentLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
getWeatherByCurrentLocation(latitude, longitude);
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
//await ์ฐ๊ณ ์ ํ๋ ํจ์๋ async ์ฌ์ผ ํ๋ค!
const getWeatherByCurrentLocation = async (lat, lon) => {
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=9007b03099d0209ad491e058a2d63c26`;
let response = await fetch(url) //๋น๋๊ธฐ(์ง๋
: url์ patchํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ!) : url ๋ก๋ฉํ ๋ค ์๋ต๊ฐ์ ๋ฐ๊ธฐ๋ก ํจ
let data = await response.json(); //์๋ต์์ json ์ถ์ถํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ
}
useEffect(() => {
getCurrentLocation()
}, []);
return (
<div>
<div>
<WeatherBox />
<WeatherButton title="Seoul" />
<WeatherButton title="NewYork" />
<WeatherButton title="Toronto" />
<WeatherButton title="Paris" />
<WeatherButton title="Rondon" />
</div>
<div>
</div>
</div>
);
}
export default App;
App.css
body{
background : url(https://wallpaperaccess.com/full/5466320.jpg);
height: 100vh;
background-repeat: no-repeat;
background-size: cover;
}
.weather-box{
color : #aec6fe;
}
WeatherButton.js
import React from 'react'
import { Button } from 'react-bootstrap';
const WeatherButton = (props) => {
return (
<div>
<Button variant="primary">{props.title}</Button>
</div>
)
}
export default WeatherButton
WeatherBox.js
import React from 'react'
const WaetherBox = () => {
return (
<div className='weather-box'>
<div>Your Weather</div>
<h2>30๋ / 230 ํ์จ</h2>
<h2>๋ง์ ํ๋</h2>
</div>
)
}
export default WaetherBox
๊ธฐํ ๋ฐ์ํ ์น์ผ๋ก ๋งํฌ์ ํ๋ ๊ณผ์ ์ ํด๋น ํฌ์คํ (ํด๋ฆญ)์ ๋ด์๋ค.
728x90