This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
forked from joel-g/lets-do-lunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
307 lines (287 loc) · 9.83 KB
/
index.android.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button,
TextInput,
Picker,
Linking,
KeyboardAvoidingView,
ActivityIndicator
} from 'react-native';
import RNGooglePlaces from 'react-native-google-places';
import Polyline from '@mapbox/polyline';
import { GOOGLE_MAPS_KEY } from 'react-native-dotenv';
import MapView from 'react-native-maps';
// import RadioForm, {RadioButton, RadioButtonInput, RadioButtonLabel} from 'react-native-simple-radio-button';
export default class LetsDoLunch extends Component {
constructor(props) {
super(props);
this.state = {
userLocation: {
latitude: null,
longitude: null
},
friendLocation: {
latitude: null,
longitude: null
},
locationData: [],
midPoint: null,
region: {},
keyword: '',
type: 'restaurant',
radius: 1609,
searching: false
};
this.onRegionChange = this.onRegionChange.bind(this);
}
findMiddle(myLoc, theirLoc) {
let meetLat;
let meetLon;
if (myLoc.latitude > 0) {
meetLat = (myLoc.latitude + theirLoc.latitude) / 2
} else {
meetLat = (myLoc.latitude - theirLoc.latitude) / 2
};
if (myLoc.longitude > 0) {
meetLon = (myLoc.longitude + theirLoc.longitude) / 2
} else {
meetLon = (myLoc.longitude - theirLoc.longitude) / 2
}
var midPoint = {
latitude: meetLat,
longitude: meetLon,
}
return midPoint;
}
pickLocation(person) {
RNGooglePlaces.openPlacePickerModal().then((place) => {
if (person === 'user') {
this.setState({userLocation: place})
} else {
this.setState({friendLocation: place})
}
// place represents user's selection from the
// suggestions and it is a simplified Google Place object.
})
.catch(error => console.log(error.message)); // error is a Javascript Error object
}
async getMidPoint(startLoc, destinationLoc) {
try {
console.log(startLoc, destinationLoc);
console.log(1);
let resp = await fetch(`https://maps.googleapis.com/maps/api/directions/json?origin=${ startLoc }&destination=${ destinationLoc }&key=${ GOOGLE_MAPS_KEY }`);
console.log(2);
let respJson = await resp.json();
console.log(respJson);
console.log(3);
let points = Polyline.decode(respJson.routes[0].overview_polyline.points);
console.log(4);
let coords = points.map((point, index) => {
return {
latitude : point[0],
longitude : point[1]
}
});
console.log(5);
console.log('coords', coords);
console.log('length', Math.floor(coords.length / 2))
let midLocation = coords[Math.floor(coords.length / 2)];
return midLocation;
} catch(error) {
return error
}
}
async findLocations(startLoc, destinationLoc) {
try {
this.setState({ searching: true });
console.log('SUP');
let midLocation = await this.getMidPoint(startLoc, destinationLoc);
console.log(midLocation);
console.log(6);
let url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=${ GOOGLE_MAPS_KEY }&location=${ midLocation.latitude },${ midLocation.longitude }&type=${this.state.type}&keyword=${ this.state.keyword }&radius=${ this.state.radius }`
let results = await fetch(url);
console.log(7, url);
let resultsJson = await results.json();
console.log(8);
console.log(resultsJson);
this.setState({
searching: false,
locationData: resultsJson.results.slice(0, 5),
midPoint: midLocation,
region: this.regionContainingPoints(resultsJson.results.slice(0, 5))
});
console.log(this.state.locationData);
} catch(error) {
return error
}
}
regionContainingPoints(points) {
var minX, maxX, minY, maxY;
// init first point
((point) => {
minX = point.geometry.location.lat;
maxX = point.geometry.location.lat;
minY = point.geometry.location.lng;
maxY = point.geometry.location.lng;
})(points[0]);
// calculate rect
points.map((point) => {
minX = Math.min(minX, point.geometry.location.lat);
maxX = Math.max(maxX, point.geometry.location.lat);
minY = Math.min(minY, point.geometry.location.lng);
maxY = Math.max(maxY, point.geometry.location.lng);
});
var midX = (minX + maxX) / 2;
var midY = (minY + maxY) / 2;
var midPoint = [midX, midY];
var deltaX = (maxX - minX) * 1.5;
var deltaY = (maxY - minY) * 1.5;
return {
latitude: midX, longitude: midY,
latitudeDelta: deltaX, longitudeDelta: deltaY,
};
}
onRegionChange(region) {
this.setState({ region });
}
render() {
let midPointButton;
let map = <View style={{flex: 1.8, backgroundColor: 'steelblue'}}>
<Text>Step 1: Set your location.</Text>
<Text>Step 2: Set your friends location.</Text>
<Text>Step 3 (optional): Add keywords for to narrow your search ('lunch', 'mexican', 'teriyaki')</Text>
<Text> </Text>
<Text>{"Note: If the results don't seem to match your keyword, try expanding your search radius."}</Text>
</View>;
if (this.state.midPoint) {
console.log('map reached')
map = <View style = {styles.container}>
<MapView
style = {styles.map}
region = {this.state.region}
onRegionChange = {this.onRegionChange}
>
{this.state.locationData.map(marker => (
<MapView.Marker
coordinate={{
latitude: marker.geometry.location.lat,
longitude: marker.geometry.location.lng
}}
title={marker.name}
description={marker.vicinity}
key={marker.id}
onCalloutPress={() => {Linking.openURL('http://www.google.com/maps/search/?api=1&query=' + marker.name.split(" ").join("+") + `&query_place_id=${marker.place_id}`)}}
/>
))}
</MapView>
</View>
}
if (this.state.userLocation.latitude && this.state.friendLocation.latitude && !this.state.searching) {
midPointButton = <Button title="Find midpoint" onPress={() => this.findLocations(this.state.userLocation.latitude.toString() + ", " + this.state.userLocation.longitude.toString(), this.state.friendLocation.latitude.toString() + ", " + this.state.friendLocation.longitude.toString())} />
}
else if (this.state.searching) {
midPointButton = <ActivityIndicator animating={true} size='large' />
}
return (
<View style={{flex: 1}}>{/* App-wide container */}
<View style={{flex: .3, backgroundColor: 'powderblue'}}>{/* Header container */}
<Text style={styles.heading}>
{"Let's Do Lunch"}
</Text>
</View>
<View style={{flex: 1, backgroundColor: 'skyblue'}}>{/* Buttons & text input (but not dropdowns) */}
<View style={{flex: 1, flexDirection: 'row'}}>
<View style={{flex: 1}}>
<Button title="Set your location" onPress={() => this.pickLocation('user')} />
</View>
<View style={{flex: 1}}>
<Button color="blue" title="Set friend's location" onPress={() => this.pickLocation('friend')} />
</View>
</View>
<View style={{flex: 1}}>
<TextInput
style={{height: 50}}
placeholder="keywords: (lunch, cocktails, Mexican, burgers)"
onChangeText={(text) => this.setState({keyword: text})}
/>
</View>
<View style={{backgroundColor: 'white', flex: 1, flexDirection: 'row'}}>
<View style={styles.picker}>
<Picker
selectedValue={this.state.type}
onValueChange={(itemValue, itemIndex) => this.setState({type:itemValue})} >
<Picker.Item label='Restaurant' value='restaurant' />
<Picker.Item label='Bar/Tavern' value='bar' />
<Picker.Item label='Café' value='cafe' />
<Picker.Item label='Park' value='park' />
</Picker>
</View>
<View style={styles.picker}>
<Picker
selectedValue={this.state.radius}
onValueChange={(itemValue, itemIndex) => this.setState({radius:itemValue})} >
<Picker.Item label='1 mi radius' value='1609' />
<Picker.Item label='2 mi radius' value='3218' />
<Picker.Item label='3 mi radius' value='4829' />
<Picker.Item label='4 mi radius' value='6437' />
<Picker.Item label='5 mi radius' value='8046' />
</Picker>
</View>
</View>
{midPointButton}
</View>
{map}
</View>
)
}
}
// var RadioButtonProject = React.createClass({
// getInitialState: function() {
// return {
// value: 0,
// }
// },
// render: function() {
// return (
// <View>
// <RadioForm
// radio_props={radio_props}
// initial={0}
// onPress={(value) => {this.setState({type:value})}}
// />
// </View>
// );
// }
// });
// const radio_props = [
// {label: 'restaurant', value: 0 },
// {label: 'tavern', value: 1 }
// ];
const styles = StyleSheet.create({
heading: {
fontSize: 30,
color: 'black',
textAlign: 'center'
},
container: {
flex: 1.8,
justifyContent: 'flex-end',
alignItems: 'center',
bottom: 0,
right: 0,
left: 0
},
map: {
...StyleSheet.absoluteFillObject,
},
picker: {
flex: 1,
borderWidth: 0.3,
borderColor: 'silver'
}
});
AppRegistry.registerComponent('LetsDoLunch', () => LetsDoLunch);