This repository was archived by the owner on Nov 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
298 lines (277 loc) · 8.38 KB
/
App.js
File metadata and controls
298 lines (277 loc) · 8.38 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
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
import React, { Component } from 'react';
import { ActivityIndicator, Button, Picker, Text, View, Alert, Dimensions, StyleSheet, TouchableOpacity } from 'react-native';
import MapView from 'react-native-maps';
import PickerFast from './Picker.js'
import TimerMixin from 'react-timer-mixin';
import env from './env.js'
import redImg from './assets/red.png';
import greenImg from './assets/green.png';
import stopImg from './assets/stop.png';
import Trip from './Trip.js';
import Places from './Places.js';
import Util from './Util.js'
import Api from './Api.js'
const util = new Util();
const api = new Api();
export default class FastGo extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
selectedMode: 'pt_pub',
selectedPlaces: 'mcdonalds',
currentPosition: env.START_LOCATION,
modes: [
{"mode": "pt_pub", "title": "Public Transport"},
{"mode": "me_car", "title": "Car"},
{"mode": "ps_tax", "title": "Taxi"}],
baseURL: "https://api.tripgo.com/v1/",
placesOptions: [],
places: [],
trips: [],
trip: null,
message: null,
region: env.REGION,
nextRegion: null,
}
}
componentDidMount() {
let updateLocation = (newLocation) => {
newLocation.latitudeDelta = 0.04;
newLocation.longitudeDelta = 0.04;
this.setState({'currentPosition': newLocation, 'region': newLocation});
}
if (env.GEOLOCATION_ENABLED) {
navigator.geolocation.getCurrentPosition(
(position) => updateLocation(position.coords),
(error) => util.log(JSON.stringify(error)),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
} else {
updateLocation(env.START_LOCATION)
}
this.setState({isLoading: false});
console.log("start");
return true;
}
update(places, placesOptions) {
if (places === undefined) {
places = this.refs.places.getPlaces();
placesOptions = this.refs.places.getPlacesOptions();
}
this.setState({
places,
placesOptions,
'message': null,
'trips': [],
'trip': null},
() => {this.map.fitToElements(true)})
}
updateTrip(trip) {
this.setState({'trip':trip},
() => {this.map.fitToElements(true)})
}
render() {
if (this.state.isLoading) {
return (
<View style={styles.container}>
<MapView
style={styles.map}
scrollEnabled={true}
zoomEnabled={true}
pitchEnabled={true}
rotateEnabled={true}
region={this.state.region}
/>
</View>
);
}
let onGoPress = () => {
this.setState({'showPlaceSelector': false,'showModeSelector': false, message: 'Computing Trips... '})
return api.computeFastestTrip(this.state.baseURL,
this.state.selectedMode,
this.state.places,
this.state.currentPosition)
.then((fastestTrip) => {
if (fastestTrip[0].routingJSON === null) {
this.setState({
message: 'Error: ' + fastestTrip[0].error.error
})
} else {
this.setState({
message: null,
trips: fastestTrip
},() => this.refs.trips.drawFaster())
}
})
.catch((error) => {
console.error(error);
});
};
let modesItems = this.state.modes.map( (s, i) => {
return <Picker.Item key={i} value={s.mode} label={s.title} />
});
let placesItems = this.state.placesOptions.map( (s, i) => {
return <Picker.Item key={i} value={s} label={s} />
});
let getPlaces = (key) => {
return this.state.places[key];
}
return (
<View style={styles.container}>
<MapView
style={styles.map}
ref={ref => { this.map = ref; }}
scrollEnabled={true}
zoomEnabled={true}
pitchEnabled={true}
rotateEnabled={true}
initialRegion={this.state.region}
>
{this.state.currentPosition !== null && this.state.trip === null &&
<MapView.Marker
title='You are here'
key='current'
image={redImg}
coordinate={this.state.currentPosition}
centerOffset={{'x': 0, 'y': -20}}
/>
}
<Places
selectedPlace={this.state.selectedPlaces}
currentPosition={this.state.currentPosition}
places={this.state.places}
ref='places'
update={this.update.bind(this)}
/>
{this.state.trip === null && this.state.places.map(place => (
<MapView.Marker
title={place.address}
key={place.key}
image={greenImg}
coordinate={place}
centerOffset={{'x': 0, 'y': -20}}
/>
))}
{this.state.trip !== null && this.state.trip.segments.map(segment => (
<MapView.Polyline
key={segment.key}
coordinates={segment.waypoints}
strokeColor={segment.color}
fillColor="rgba(255,255,255,0.5)"
strokeWidth={5}
/>
))}
{this.state.trip !== null && this.state.trip.start !== null &&
<MapView.Marker
key={this.state.trip.start.key}
image={redImg}
coordinate={this.state.trip.start}
centerOffset={{'x': 0, 'y': -20}}
/>
}
{this.state.trip !== null && this.state.trip.end !== null &&
<MapView.Marker
key={this.state.trip.end.key}
image={greenImg}
coordinate={this.state.trip.end}
centerOffset={{'x': 0, 'y': -20}}
/>
}
{this.state.trip !== null && this.state.trip.stops.map(stop => (
<MapView.Marker
key={stop.key}
image={stopImg}
coordinate={stop}
/>
))}
</MapView>
<View style={styles.buttonContainer}>
<PickerFast
items={this.state.modes}
selectedItem={this.state.selectedMode}
onValueChange={(mode) => this.setState({selectedMode: mode},
() => this.update())}
/>
<PickerFast
items={this.state.placesOptions}
selectedItem={this.state.selectedPlaces}
onValueChange={(place) => this.setState({selectedPlaces: place},
() => this.update())}
/>
<TouchableOpacity
onPress={onGoPress}
style={[styles.bubble, styles.button]}
>
<Text>FastGo!</Text>
</TouchableOpacity>
</View>
<View style={styles.mapSpace}>
</View>
<View style={styles.swiper}>
<Trip
trips={this.state.trips}
ref='trips'
update={this.updateTrip.bind(this)}
/>
{this.state.message !== null &&
<View style={[styles.bubble, styles.message]}>
<Text style={{ textAlign: 'center' }}>
{this.state.message}
</Text>
</View>
}
</View>
</View>
);
}
}
const { width, height } = Dimensions.get('window');
const SCREEN_WIDTH = width;
const SCREEN_HEIGHT = height;
const ASPECT_RATIO = width / height;
const styles = StyleSheet.create({
container: {
flex: 1,
},
buttonContainer: {
flex: 0.1,
flexDirection: 'row',
marginTop: 20,
backgroundColor: 'transparent',
alignItems: 'center',
},
mapSpace: {
flex: 0.75,
zIndex: -1
},
swiper: {
flex: 0.15,
},
bubble: {
backgroundColor: 'rgba(255,255,255,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
},
button: {
flex: 0.33,
paddingHorizontal: 12,
alignItems: 'center',
marginHorizontal: 10,
},
map: {
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT,
...StyleSheet.absoluteFillObject,
},
message: {
width: SCREEN_WIDTH,
alignItems: 'stretch',
marginTop: 'auto'
},
selector: {
backgroundColor: 'rgba(255,255,255,0.7)',
marginTop: 'auto'
}
});