-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
233 lines (219 loc) · 6.25 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
import React, { Component } from "react";
import {
TextInput,
StyleSheet,
Text,
View,
Keyboard,
TouchableHighlight
} from "react-native";import MapView, { Polyline, Marker } from "react-native-maps";
import Constants from "./Constants";
import MapConfig from "./MapConfig";
import _ from "lodash";
import PolyLine from "@mapbox/polyline";
import Geolocation from 'react-native-geolocation-service';
import haversine from 'haversine';
let pointCoords = [];
let points = [];
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
error: "",
latitude: 12.9166,
longitude: 77.6101,
destination: "",
predictions: [],
pointCoords: []
};
this.onChangeDestinationDebounced = _.debounce(
this.onChangeDestination,
1000
);
}
componentDidMount() {
//Get current location and set initial region to this
// navigator.geolocation.getCurrentPosition(
// position => {
// this.setState({
// latitude: position.coords.latitude,
// longitude: position.coords.longitude
// });
// },
// error => console.error(error),
// { enableHighAccuracy: true, maximumAge: 2000, timeout: 20000 }
// );
Geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
},
(error) => { },
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
this.watchID = Geolocation.watchPosition(
position => {
//const { coordinate, routeCoordinates, distanceTravelled } = this.state;
const { latitude, longitude } = position.coords;
// let exist = _.findIndex(pointCoords, (o) => {
// return _.isMatch(o, { latitude : position.coords.latitude, longitude:position.coords.longitude})
// }) > -1;
let inRoad = false;
_.forEach(pointCoords, function(value) {
if( inRoad ) return;
let roadAndPos = haversine(value,{latitude:latitude, longitude:longitude});
inRoad = roadAndPos<0.02 ? true : false;
});
if(!inRoad && this.state.place_id){
this.setState({latitude, longitude},
()=>this.getRouteDirections( this.state.destination )
)
}
// alert(exist)
// const newCoordinate = {
// latitude,
// longitude
// };
},{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
}
async getRouteDirections( destinationName) {
try {
const response = await fetch(
`https://maps.googleapis.com/maps/api/directions/json?origin=${
this.state.latitude
},${
this.state.longitude
}&destination=place_id:${this.state.place_id}&key=${Constants.apiKey}`
);
const json = await response.json();
points = PolyLine.decode(json.routes[0].overview_polyline.points);
pointCoords = points.map(point => {
return { latitude: point[0], longitude: point[1] };
});
this.setState({
pointCoords,
predictions: [],
destination: destinationName
});
Keyboard.dismiss();
this.map.fitToCoordinates(pointCoords);
} catch (error) {
console.error(error);
}
}
async onChangeDestination(destination) {
const apiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${Constants.apiKey}
&input=${destination}&location=${this.state.latitude},${
this.state.longitude
}&radius=2000`;
console.log(apiUrl);
try {
const result = await fetch(apiUrl);
const json = await result.json();
this.setState({
predictions: json.predictions
});
console.log(json);
} catch (err) {
console.error(err);
}
}
render() {
let marker = null;
if (this.state.pointCoords.length > 1) {
marker =[
<Marker key={'destination'}
//icon={'./imgs/'}
coordinate={this.state.pointCoords[this.state.pointCoords.length - 1]}
/>,
<Marker key={'source'}
coordinate={{latitude: this.state.latitude, longitude: this.state.longitude}}
/>
];
}
const predictions = this.state.predictions.map(prediction => (
<TouchableHighlight
onPress={() =>
this.setState({
place_id: prediction.place_id
},()=>
this.getRouteDirections(
prediction.structured_formatting.main_text
))
}
key={prediction.id}
>
<View>
<Text style={styles.suggestions}>
{prediction.structured_formatting.main_text}
</Text>
</View>
</TouchableHighlight>
));
return (
<View style={styles.container}>
<MapView
customMapStyle={MapConfig}
ref={map => {
this.map = map;
}}
style={styles.map}
region={{
latitude: this.state.latitude,
longitude: this.state.longitude,
latitudeDelta: 0.015,
longitudeDelta: 0.0121
}}
showsUserLocation={true}
>
<Polyline
coordinates={this.state.pointCoords}
strokeWidth={5}
strokeColor="#000"
/>
{marker}
</MapView>
<TextInput
placeholder="Enter destination..."
style={styles.destinationInput}
value={this.state.destination}
clearButtonMode="always"
onChangeText={destination => {
console.log(destination);
this.setState({ destination });
this.onChangeDestinationDebounced(destination);
}}
/>
{predictions}
</View>
);
}
}
const styles = StyleSheet.create({
suggestions: {
backgroundColor: "white",
padding: 5,
fontSize: 18,
borderWidth: 0.5,
marginLeft: 5,
marginRight: 5
},
destinationInput: {
height: 40,
borderWidth: 0.5,
marginTop: 50,
marginLeft: 5,
marginRight: 5,
padding: 5,
backgroundColor: "white"
},
container: {
...StyleSheet.absoluteFillObject
},
map: {
...StyleSheet.absoluteFillObject
}
});