-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
55 lines (45 loc) · 1.49 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
// import { StatusBar } from "expo-status-bar";
import React, {Component} from "react";
import Loading from "./Loading.js";
import {Alert} from "react-native";
import axios from "axios";
import * as Location from "expo-location";
import Weather from "./Weather";
const API_KEY = "041c1b827d48b19d47adf99658ff1ddc"
export default class App extends Component {
state = {
isLoading : true
}
getweather = async(latitude, longitude) => {
const { data : {
main : {temp},
weather
} } = await axios.get(
// 왜 "나 '가 아니라 ` (back-tic) 인가?? 문자열에 변수를 포함시키기 위함임.
`http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`)
// console.log(weather)
this.setState({
isLoading: false,
temp,
condition: weather[0].main})
}
geolocation = async() => {
try {
await Location.requestPermissionsAsync()
const {coords : {latitude, longitude}} = await Location.getCurrentPositionAsync()
// Send the coordinates to API, and get weather
this.getweather(latitude, longitude)
// this.setState({isLoading: false})
} catch (error) {
console.log(error)
Alert.alert("Can't Find you!", "So Sad")
}
}
componentDidMount() {
this.geolocation();
}
render() {
const {isLoading, temp, condition} = this.state
return isLoading ? <Loading /> : <Weather temp={Math.round(temp)} conditions={condition}/>
}
}