-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
133 lines (121 loc) · 3.35 KB
/
App.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { API_KEY } from "@env";
import * as Location from "expo-location";
import { StatusBar } from "expo-status-bar";
import { useEffect, useState } from "react";
import {
Dimensions,
ScrollView,
StyleSheet,
Text,
View,
ActivityIndicator,
} from "react-native";
export default function App() {
const [city, setCity] = useState("Loading...");
const [days, setDays] = useState([]);
const [ok, setOk] = useState(true);
const getWeather = async () => {
const KEY = API_KEY;
//console.log(KEY);
const { granted } = await Location.requestForegroundPermissionsAsync();
if (!granted) {
setOk(false);
}
const {
coords: { latitude, longitude },
} = await Location.getCurrentPositionAsync({ accuracy: 5 });
//console.log(latitude, longitude);
const location = await Location.reverseGeocodeAsync(
{ latitude, longitude },
{ useGoogleMaps: false }
);
//console.log(location);
setCity(location[0].city);
// const response = await fetch(
// `api.openweathermap.org/data/2.5/forecast/daily?lat=44.34&lon=10.99&cnt=7&appid=${KEY}}`
// );
// const json = await response.json();
// console.log(json);
const response = await fetch(
`http://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=${KEY}&units=metric`
);
const json = await response.json();
//console.log(json.list[0].weather[0].description, json.list[0].main.temp);
setDays(json.list);
};
useEffect(() => {
getWeather();
});
//View는 container
return (
<View style={styles.container}>
{!ok ? (
<View style={styles.city}>
<Text style={styles.cityName}>위치 찾을 수 없음</Text>
</View>
) : (
<View style={styles.city}>
<Text style={styles.cityName}>{city}</Text>
</View>
)}
<ScrollView
pagingEnabled
horizontal
contentContainerStyle={styles.weather}
showsHorizontalScrollIndicator={false}
>
{days.length === 0 ? (
<View style={styles.day}>
<ActivityIndicator
color="black"
size="large"
style={{ marginTop: 10 }}
/>
</View>
) : (
days.map((day, index) => (
<View key={index} style={styles.day}>
<Text style={styles.temp}>
{/* {day.main.temp} */}
{parseFloat(day.main.temp).toFixed(1)}℃
</Text>
<Text style={styles.description}>{day.weather[0].main}</Text>
<Text style={{ marginBottom: -70 }}>
{day.weather[0].description}
</Text>
</View>
))
)}
</ScrollView>
<StatusBar style="black"></StatusBar>
</View>
);
}
const { width: SCREEN_WIDTH } = Dimensions.get("window");
// = const SCREEN_WIDTH = const Dimensions.get('window').width;
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: "gold" },
city: {
flex: 1.2,
justifyContent: "center",
alignItems: "center",
},
cityName: {
color: "black",
fontSize: 50,
fontWeight: "500",
},
weather: {},
day: {
width: SCREEN_WIDTH,
alignItems: "center",
},
temp: {
marginTop: 50,
fontSize: 128,
},
description: {
marginTop: 10,
fontSize: 50,
},
});