-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavigationMap.js
173 lines (156 loc) · 4.74 KB
/
NavigationMap.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
import React, { Component } from 'react';
import {
View,
StyleSheet,
PermissionsAndroid,
ActivityIndicator,
Image,
TouchableOpacity,
} from 'react-native';
import MapView from 'react-native-maps-with-navigation';
import MapViewDirections from 'react-native-maps-directions';
export default class NavigationMap extends Component {
constructor(props) {
super(props);
this.state = {
prevPos: null,
curPos: null,
rideHasStarted: false,
}
this.watchPosition = this.watchPosition.bind(this);
this.getRotation = this.getRotation.bind(this);
this.updateMap = this.updateMap.bind(this);
this.onMapReady = this.onMapReady.bind(this);
this.startRide = this.startRide.bind(this);
}
async componentDidMount() {
await this.watchPosition();
}
async watchPosition() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
'title': 'GPS Permission',
'message': 'The app needs GPS access to work properly.'
});
if (granted !== PermissionsAndroid.RESULTS.GRANTED)
throw 'GPS Permission Denied!';
const watchId = navigator.geolocation.watchPosition(pos => {
const { latitude, longitude } = pos.coords;
const curPos = { latitude, longitude };
const prevPos = this.state.curPos;
const curReg = {
latitude: curPos.latitude,
longitude: curPos.longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
};
const curRot = this.getRotation(prevPos, curPos);
const curAng = 45;
this.updateMap(curPos, curReg, curRot, curAng);
this.setState({ prevPos, curPos });
}, err => { console.log(err) },
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000, distanceFilter: 2 });
} catch (err) { console.log(err); }
}
getRotation(prevPos, curPos) {
if (!prevPos) return 0;
const xDiff = curPos.latitude - prevPos.latitude;
const yDiff = curPos.longitude - prevPos.longitude;
return (Math.atan2(yDiff, xDiff) * 180.0) / Math.PI;
}
updateMap(pos, reg, rot, ang) {
if (this.refs.map && this.state.rideHasStarted) {
this.refs.map.animateToNavigation(pos, rot, ang);
}
}
onMapReady() {
const padding = 150;
const opt = { edgePadding: { top: padding, right: padding, bottom: padding, left: padding } };
this.refs.map.fitToCoordinates([this.state.curPos, this.props.destination], opt);
}
startRide() {
const reg = {
latitude: this.state.curPos.latitude,
longitude: this.state.curPos.longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
};
this.refs.map.animateToRegion(reg);
this.setState({ rideHasStarted: true })
}
render() {
if (!this.state.curPos) return (
<View style={styles.loading}>
<ActivityIndicator size="large" color={this.props.color || "#F54A39"} />
</View>
);
return (
<View style={styles.flex}>
<MapView
ref="map"
rotateEnabled={!this.state.rideHasStarted}
scrollEnabled={!this.state.rideHasStarted}
zoomEnabled={!this.state.rideHasStarted}
style={[styles.flex, this.props.style]}
initialRegion={{
...this.state.curPos,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
}}
onMapReady={this.onMapReady}
toolbarEnabled={true}
>
<MapView.Marker
coordinate={this.state.curPos}
anchor={{ x: 0.5, y: 0.5 }}
image={this.props.driverImage || null}
/>
<MapView.Marker
coordinate={this.props.destination}
pinColor={this.props.color}
/>
<MapViewDirections
origin={this.state.curPos}
destination={this.props.destination}
apikey={this.props.googleMapsApiKey}
strokeWidth={this.props.strokeWidth || 5}
strokeColor={this.props.color || "#F54A39"}
/>
</MapView>
{ !this.state.rideHasStarted &&
<TouchableOpacity
style={styles.startButton}
onPress={this.startRide}
activeOpacity={0.8}>
<Image
style={{width: '100%', height: '100%'}}
source={require('./images/start-icon.png')}
tintColor={this.props.color || "#F54A39"}
/>
</TouchableOpacity>
}
</View>
);
}
}
const styles = StyleSheet.create({
flex: {
flex: 1,
width: '100%',
position: 'relative',
},
loading: {
position: 'absolute',
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
startButton: {
position: 'absolute',
height: 100,
width: 100,
bottom: 15,
left: 0,
}
});