-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
89 lines (72 loc) · 2.43 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
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, FlatList } from 'react-native';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';
export default class App extends React.Component{
state = {
hasLocationPermission: false,
latitude: 0,
longitude: 0,
restaurantList: []
}
componentDidMount() {
this.getLocationAsync();
};
async getLocationAsync () {
const { status } = await Permissions.askAsync(
Permissions.LOCATION
);
if (status === 'granted') {
let location = await Location.getCurrentPositionAsync({});
this.setState({
hasLocationPermissions: true,
latitude: location.coords.latitude,
longitude: location.coords.longitude,
});
} else {
alert('Location permission not granted');
}
};
handleRestaurantSearch = () => {
const location = `location=${this.state.latitude},${this.state.longitude}`;
fetch(`https://travel-advisor.p.rapidapi.com/restaurants/list-by-latlng?latitude=${this.state.latitude}&longitude=${this.state.longitude}&limit=30¤cy=USD&distance=2000&open_now=false&lunit=km&lang=en_US`, {
"method": "GET",
"headers": {
"x-rapidapi-host": "travel-advisor.p.rapidapi.com",
"x-rapidapi-key": "66793091c5mshccead1a3d06a524p1059b8jsnc99793b18717"
}
})
.then(response => response.json())
.then(results => this.setState({restaurantList: results}))
.catch(err => {
console.error(err);
});
}
render() {
console.log(this.state.restaurantList)
return (
<View style={this.styles.container}>
<FlatList
data={this.state.restaurantList}
renderItem={({item}) => (
<Text>{item.name}</Text>
)}
style={{backgroundColor: 'grey', width: '80%', margin: 60, padding: 5}}
/>
<TouchableOpacity onPress={() => this.handleRestaurantSearch()}>
<Text style={{backgroundColor: 'grey', color: 'white', padding: 20, marginBottom: 50}}>Search Restaurants</Text>
</TouchableOpacity>
<StatusBar style="auto" />
</View>
);
}
styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
}