Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
52 lines (48 loc) · 1.33 KB
/
Copy pathApp.js
File metadata and controls
52 lines (48 loc) · 1.33 KB
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
import React from 'react';
import {Alert} from 'react-native';
import Loading from './Loading';
import Weather from './Weather';
import * as Location from 'expo-location';
import axios from 'axios';
const API_KEY = "181223d7949376695813e74a979326fe"; // openweathermap API_KEY
export default class extends React.Component {
state = {
isLoading : true
};
getWeather = async (latitude, longitude) => {
const {
data : {
main : {temp},
weather,
name
}
} = await axios.get(
`http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&APPID=${API_KEY}&units=metric`
);
this.setState({
isLoading : false,
temp,
condition : weather[0].main,
location : name
})
};
getLocation = async() => {
try {
// throw Error;
await Location.requestPermissionsAsync();
const {
coords: {latitude, longitude}
} = await Location.getCurrentPositionAsync();
this.getWeather(latitude, longitude);
} catch (error) {
Alert.alert("Can't find you.", "So sad");
}
};
componentDidMount(){
this.getLocation();
}
render() {
const { isLoading, temp, condition, location } = this.state;
return isLoading ? <Loading /> : <Weather temp={Math.round(temp)} condition={condition} location={location} />;
}
}