-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
106 lines (97 loc) · 2.76 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
import React from "react";
import { StyleSheet, Text, View, InteractionManager } from "react-native";
import { MapView } from "expo";
import { Ionicons } from "@expo/vector-icons";
import { setInterval } from "core-js/library/web/timers";
import Foli from "./Foli";
import MisaMaOlen from "./MisaMaOlen";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
folit: [],
region: {
latitude: 60.4503888,
longitude: 22.2617936,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
},
};
this.fetchFolit = this.fetchFolit.bind(this);
this.onRegionChange = this.onRegionChange.bind(this);
this.hasValidLocation = this.hasValidLocation.bind(this);
this.isInRange = this.isInRange.bind(this);
}
async componentDidMount() {
await this.fetchFolit();
setInterval(this.fetchFolit, 5000);
}
hasValidLocation(vehicle) {
return (
vehicle.latitude &&
vehicle.latitude > 0 &&
vehicle.longitude &&
vehicle.longitude > 0
);
}
isInRange(latOrLong, delta, valueToCheck) {
return (
latOrLong - 0.5 * delta <= valueToCheck &&
valueToCheck <= latOrLong + 0.5 * delta
);
}
hasLocationInRegion(vehicle, region) {
if (vehicle.publishedlinename == "73") {
const a = 1;
}
return (
this.isInRange(region.latitude, region.latitudeDelta, vehicle.latitude) &&
this.isInRange(region.longitude, region.longitudeDelta, vehicle.longitude)
);
}
async fetchFolit() {
const response = await fetch("https://data-new.foli.fi/siri-test/vm");
const json = await response.json();
const vehicles = Object.values(json.result.vehicles);
let validVehicles = vehicles.filter(vehicle =>
this.hasValidLocation(vehicle)
);
validVehicles = validVehicles.map(vehicle => {
return {
...vehicle,
isInView: this.hasLocationInRegion(vehicle, this.state.region),
};
});
const vehiclesInView = validVehicles.filter(vehicle => vehicle.isInView);
console.log("Vehicle count: " + vehiclesInView.length);
InteractionManager.runAfterInteractions(() => {
this.setState({
folit: validVehicles,
});
});
}
onRegionChange(region) {
this.setState({ region });
}
render() {
return (
<MapView
style={{ flex: 1 }}
initialRegion={this.state.region}
onRegionChange={this.onRegionChange}
>
{this.state.folit.map(foli => {
return (
<Foli
key={foli.vehicleref}
name={foli.publishedlinename}
latitude={foli.latitude}
longitude={foli.longitude}
/>
);
})}
<MisaMaOlen />
</MapView>
);
}
}