-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
447 lines (433 loc) · 12.2 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import React, { Component } from 'react';
import {
StyleSheet,
ScrollView,
PropTypes,
Text,
TouchableHighlight,
View,
TabBarIOS,
ListView,
} from 'react-native';
import '@expo/vector-icons'; // 5.2.0
import 'prop-types'; // 15.6.0
import { MapView } from 'expo';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
markers: props.markers,
routes: props.routes,
tempMarkers: [],
selectedTab: 'one',
};
this.processCallback = this.processCallback.bind(this);
this.processRoutesCallback = this.processRoutesCallback.bind(this);
}
componentDidMount() {
//this.refresh();
/* Not quite working?
setTimeout(
() => { this.refresh(); },
5000
);
*/
}
render() {
return (
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={{
latitude: 25.8011413,
longitude: -80.2044014,
latitudeDelta: 0.18,
longitudeDelta: 0.18,
}}>
{this.state.tempMarkers.map(this.renderPart)}
</MapView>
<TouchableHighlight
style={styles.button}
onPress={() => this.refresh()}
underlayColor="#bbbbbb">
<Text style={styles.buttonText}>
Refresh
</Text>
</TouchableHighlight>
</View>
);
}
renderPart(tempMarker, i) {
var lat = tempMarker.coordinate.latitude;
var lon = tempMarker.coordinate.longitude;
return (
/* Sample MapView.Marker format accepted
[
{
"coordinate":{
"latitude":25.77553,
"longitude":-80.140643
},
"title":"MB621",
"description":"ID: 1029047 Direction: N"
},
]
*/
(
<MapView.Marker
coordinate={{ latitude: lat, longitude: lon }}
title={tempMarker.title}
description={tempMarker.description}
/>
)
);
}
refresh() {
//this.displayTrolleys();
//this.displayRoutes();
//this.displayRoutesAsMarkers();
this.setState({ tempMarkers: [] });
//this.requestMiamiBeachTrolleys(this.processCallback);
//this.requestMiamiTrolleys(this.processCallback);
this.requestMiamiTrolleyRoutes(this.processRoutesCallback);
}
processCallback(newMarkers) {
//console.log('newMarkers: ' + JSON.stringify(newMarkers));
this.setState({ tempMarkers: this.state.tempMarkers.concat(newMarkers) });
}
processRoutesCallback(routesJSON) {
//console.log('routesJSON: ' + JSON.stringify(routesJSON.routes));
var stops = routesJSON.stops;
var stopMarkers = [];
var i = 0;
for (i = 0; i < stops.length; i++) {
stopMarkers.push({
coordinate: {
latitude: parseFloat(stops[i].Latitude),
longitude: parseFloat(stops[i].Longitude),
},
title: 'Route '+stops[i].RouteId+' Stop '+stops[i].StopNumber,
description: 'Sequence: ' +
stops[i].Sequence +
' Description: ' +
stops[i].Description,
});
}
this.setState({ tempMarkers: this.state.tempMarkers.concat(stopMarkers) });
}
requestMiamiBeachTrolleys(callback) {
console.log('requestMiamiBeachTrolleys() called');
var url =
'http://app.tsomobile.com/rest/MappingController/GetUnitFromRouteAntibunching';
var tkn = '825894C5-2B5F-402D-A055-88F2297AF99A';
this.sendTSOTrolleyRequest(url, tkn, '38836', callback);
this.sendTSOTrolleyRequest(url, tkn, '40756', callback);
this.sendTSOTrolleyRequest(url, tkn, '72486', callback);
this.sendTSOTrolleyRequest(url, tkn, '73772', callback);
}
requestMiamiTrolleys(callback) {
console.log('requestMiamiTrolleys() called');
var url =
'http://app.tsomobile.com/rest/MappingController/GetUnitFromRouteAntibunching';
var tkn = '81E39EC9-D773-447E-BE29-D7F30AB177BC';
this.sendTSOTrolleyRequest(url, tkn, '71269', callback);
this.sendTSOTrolleyRequest(url, tkn, '71276', callback);
this.sendTSOTrolleyRequest(url, tkn, '71304', callback);
this.sendTSOTrolleyRequest(url, tkn, '71322', callback);
this.sendTSOTrolleyRequest(url, tkn, '71342', callback);
this.sendTSOTrolleyRequest(url, tkn, '73803', callback);
this.sendTSOTrolleyRequest(url, tkn, '73789', callback);
this.sendTSOTrolleyRequest(url, tkn, '71345', callback);
this.sendTSOTrolleyRequest(url, tkn, '75187', callback);
this.sendTSOTrolleyRequest(url, tkn, '81209', callback);
this.sendTSOTrolleyRequest(url, tkn, '71344', callback);
}
/* Process the JSON data from TSO. Data to process looks like this:
{
"Units":[
{
"MarkerID":"1029047",
"MarkerName":"MB621",
"Latitude":"25.777326",
"Longitude":"-80.140849",
"Direction":"S",
"Heading":"181"
},
],
}
*/
sendTSOTrolleyRequest(url, tkn, geofencesid, callback) {
console.log(
'sendTSOTrolleyRequest() called with geofencesid: ' + geofencesid
);
fetch(url + '?tkn=' + tkn + '&geofencesid=' + geofencesid + '&lan=en')
.then(function(res) {
console.log('TSO call result returned');
return res.json();
})
.then(function(resJson) {
console.log('TSO call end');
var routeMarkers = [];
var resultJson = JSON.parse(resJson);
//console.log(JSON.stringify(resultJson));
var trolleys = resultJson.Units;
var i = 0;
for (i = 0; i < trolleys.length; i++) {
routeMarkers.push({
coordinate: {
latitude: parseFloat(trolleys[i].Latitude),
longitude: parseFloat(trolleys[i].Longitude),
},
title: trolleys[i].MarkerName,
description: 'ID: ' +
trolleys[i].MarkerID +
' Direction: ' +
trolleys[i].Direction,
});
}
callback(routeMarkers);
})
.catch(error => {
console.log('ERROR requesting TSO trolley data');
console.warn(error);
});
}
requestMiamiTrolleyRoutes(callback) {
console.log('requestMiamiTrolleyRoutes() called');
fetch(
'http://app.tsomobile.com/rest/Routes/GetRouteFromToken?tkn=81E39EC9-D773-447E-BE29-D7F30AB177BC&routeId=-1'
)
.then(function(res) {
console.log('TSO route result returned');
return res.json();
})
.then(function(resJson) {
console.log('TSO route call end');
var resultJson = JSON.parse(resJson);
//console.log(JSON.stringify(resultJson.routes));
callback(resultJson);
})
.catch(error => {
console.log('ERROR requesting TSO trolley routes data');
console.warn(error);
});
}
displayTrolleys() {
fetch('https://miami-transit-api.herokuapp.com/api/trolley/vehicles.json')
.then(response => response.json())
.then(responseJson => {
var newMarkers = [];
var trolleys = responseJson.get_vehicles;
var count = trolleys.length;
for (var i = 0; i < count; i++) {
trolleys[i].receiveTime = new Date(
trolleys[i].receiveTime
).toLocaleString();
newMarkers.push({
latitude: trolleys[i].lat,
longitude: trolleys[i].lng,
title: 'Vehicle ID: ' + trolleys[i].equipmentID,
subtitle: 'Route: ' +
trolleys[i].routeID +
', Time: ' +
trolleys[i].receiveTime,
});
}
this.setState({ markers: newMarkers });
})
.catch(error => {
console.error(error);
});
}
displayRoutes() {
console.log('displayRoutes() called');
fetch(
'https://raw.githubusercontent.com/qtrandev/OneBusAway/master/GTFS/Miami/shapes.txt'
)
.then(response => response.text())
.then(responseText => {
console.log('ResponseText: ' + responseText);
var routeOverlays = processShapeData(responseText);
console.log('routeOverlays size = ' + routeOverlays.length);
this.setState({ routes: routeOverlays });
})
.catch(error => {
console.warn(error);
});
}
displayRoutesAsMarkers() {
console.log('displayRoutesAsMarkers() called');
fetch(
'https://raw.githubusercontent.com/qtrandev/OneBusAway/master/GTFS/Miami/shapes.txt'
)
.then(response => response.text())
.then(responseText => {
var routeMarkers = processTemporaryMarkers(responseText);
this.setState({ tempMarkers: routeMarkers });
})
.catch(error => {
console.warn(error);
});
}
}
function processTemporaryMarkers(allText) {
console.log('processTemporaryMarkers() called');
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var routes = [];
for (var i = 1; i < allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length >= 4) {
if (routes[data[0]] === undefined) {
routes[data[0]] = [];
}
routes[data[0]].push(data);
}
}
var routeMarkers = [];
for (var index in routes) {
var route = routes[index];
for (var j = 0; j < route.length; j++) {
routeMarkers.push({
coordinate: {
latitude: parseFloat(route[j][1]),
longitude: parseFloat(route[j][2]),
},
title: 'Route ' + route[j][0],
description: 'Stop sequence: ' + route[j][3],
});
}
}
return routeMarkers;
}
function processShapeData(allText) {
console.log('processShapeData() called');
var allTextLines = allText.split(/\r\n|\n/);
console.log('headers: ' + allTextLines[0]);
console.log('line last: ' + allTextLines[allTextLines.length - 2]);
var headers = allTextLines[0].split(',');
var routes = [];
for (var i = 1; i < allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length >= 4) {
if (routes[data[0]] === undefined) {
routes[data[0]] = [];
}
routes[data[0]].push(data);
}
}
var routeOverlays = [];
for (var index in routes) {
var route = routes[index];
var coordinates = [];
for (var j = 0; j < route.length; j++) {
coordinates[j] = {
latitude: parseFloat(route[j][1]),
longitude: parseFloat(route[j][2]),
};
}
routeOverlays.push({
coordinates: coordinates,
strokeColor: routeColors[route[0][0]],
lineWidth: 6,
});
}
return routeOverlays;
}
var routeColors = {
'1': '#a64598',
'2': '#679844',
'3': '#0faed2',
'4': '#3e5ba6',
'5': '#f59640',
'6': '#c73136',
'7': '#f39690',
'11': '#FFAB5F',
'12': '#A07D5C',
'13': '#2BAA33',
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
tabcontainer: {
flex: 1,
marginBottom: 50,
},
map: {
flex: 1,
},
button: {
height: 36,
backgroundColor: '#123456',
justifyContent: 'center',
alignItems: 'center',
padding: 10,
margin: 2,
borderRadius: 5,
},
buttonText: {
fontSize: 18,
color: '#fff',
},
rowText1: {
padding: 10,
fontSize: 36,
color: 'blue',
},
rowText2: {
padding: 10,
fontSize: 24,
color: 'green',
},
});
App.defaultProps = {
// not working?
markers: [],
routes: [],
tempMarkers: [],
};
class RoutesView extends Component {
constructor(props) {
super(props);
var ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
dataSource: ds.cloneWithRows([
'Allapattah',
'Biscayne',
'Coral Way',
'Health District',
'Overtown',
'Stadium',
'Coconut Grove',
'Little Havana',
'Wynwood',
]),
};
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={rowData => <Text style={styles.rowText1}>{rowData}</Text>}
/>
);
}
}
class FavoritesView extends Component {
constructor(props) {
super(props);
var ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
dataSource: ds.cloneWithRows(['Allapattah', 'Biscayne']),
};
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={rowData => <Text style={styles.rowText2}>{rowData}</Text>}
/>
);
}
}