-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapViewCard.js
More file actions
243 lines (220 loc) · 7.09 KB
/
MapViewCard.js
File metadata and controls
243 lines (220 loc) · 7.09 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
import * as React from 'react';
import MapView from 'react-native-maps';
import MapViewDirections from 'react-native-maps-directions';
import {PROVIDER_GOOGLE, Marker, Polyline} from 'react-native-maps';
import {StyleSheet, Text, View, Dimensions, Alert} from 'react-native';
import {decode} from '@mapbox/polyline';
// your google api key
const KEY = '';
export const MapViewCard = ({
height,
width,
longitude,
accuracy,
latitude,
startLat,
endLat,
startLong,
endLong,
startRouting = () => {},
showResult = () => {},
}) => {
const [coords, setCoords] = React.useState([]);
const [error, setError] = React.useState(false);
const [ready, setReady] = React.useState(false);
const [end, setEnd] = React.useState({latitude: endLat, longitude: endLong});
const [start, setStart] = React.useState({
latitude: startLat,
longitude: startLong,
});
const [startDetails, setStartDetails] = React.useState();
const [youDetails, setYouDetails] = React.useState();
// for calculating the delta for zooming of map on render
accuracy = accuracy / 2;
const circumference = 40075;
const oneDegreeOfLatitudeInMeters = 111.32 * 1000;
const angularDistance = accuracy / circumference;
const latitudeDelta = accuracy / oneDegreeOfLatitudeInMeters;
const longitudeDelta = Math.abs(
Math.atan2(
Math.sin(angularDistance) * Math.cos(startLat),
Math.cos(angularDistance) - Math.sin(startLat) * Math.sin(startLat),
),
);
//get location using reverse geolocation
const getLocationName = async (lat, long) => {
let resp = await fetch(
`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&key=${KEY}`,
);
let respJson = await resp.json();
return {
district: respJson?.results[0]?.address_components[2] ?? {},
street: respJson?.results[2]?.formatted_address ?? {},
};
};
//get intermediate coordinates between 2 locations
const getDirections = async (startLoc, destinationLoc) => {
try {
//put your API key here.
//otherwise, you'll have an 'unauthorized' error.
let resp = await fetch(
`https://maps.googleapis.com/maps/api/directions/json?origin=${startLoc}&destination=${destinationLoc}&key=${KEY}`,
);
let respJson = await resp.json();
let points = decode(respJson.routes[0].overview_polyline.points);
let coords = points.map((point, index) => {
return {
latitude: point[0],
longitude: point[1],
};
});
return coords;
} catch (error) {
return error;
}
};
// function called to return details to parent component
const returnNames = async () => {
const startResult = await getLocationName(
start?.latitude,
start?.longitude,
);
const endResult = await getLocationName(end?.latitude, end?.longitude);
showResult({
start: startResult,
end: endResult,
startDuration: startDetails,
youDuration: youDetails,
});
};
// called every time a start or end (A or B) location is changed
React.useEffect(() => {
end && start && ready && returnNames();
}, [end, start, ready]);
React.useEffect(() => {
//fetch the coordinates and then store its value into the coords Hook.
getDirections(
`${start.latitude},${start.longitude}`,
`${end.latitude},${end.longitude}`,
)
.then(coords => setCoords(coords))
.catch(err => Alert('Something went wrong'));
}, [start, end]);
return (
<View style={styles.container}>
<MapView
provider={PROVIDER_GOOGLE}
showsUserLocation={true}
zoomControlEnabled={true}
initialRegion={{
latitude: latitude,
longitude: longitude,
latitudeDelta: latitudeDelta,
longitudeDelta: longitudeDelta,
}}
loadingEnabled={true}
loadingIndicatorColor={'rgba(102, 92, 209, 1)'}
style={{
height: styles.map.height,
width: styles.map.width,
}}>
<MapViewDirections
origin={{latitude: start.latitude, longitude: start.longitude}}
destination={{latitude: end.latitude, longitude: end.longitude}}
waypoints={coords.length > 2 ? coords.slice(1, -1) : undefined}
apikey={KEY}
strokeWidth={3}
splitWaypoints={true}
showsTraffic={true}
strokeColor="hotpink"
timePrecision={'now'}
optimizeWaypoints={true}
onStart={params => {
startRouting();
}}
onReady={result => {
setReady(true);
setStartDetails({
distance: result.distance,
duration: result.duration,
});
}}
onError={() => setError(true)}
/>
<MapViewDirections
origin={{latitude: latitude, longitude: longitude}}
destination={{latitude: start.latitude, longitude: start.longitude}}
apikey={KEY}
strokeWidth={6}
strokeColor="rgba(102, 92, 209, 1)"
timePrecision={'now'}
optimizeWaypoints={true}
onReady={result => {
setYouDetails({
distance: result.distance,
duration: result.duration,
});
}}
/>
{error && (
//if there is error getting direction just display a straight line
<Polyline
zIndex={1}
coordinates={[
{latitude: latitude, longitude: longitude},
{latitude: start.latitude, longitude: start.longitude},
]}
strokeColor={'rgba(102, 92, 209, 1)'}
strokeColors={['rgba(102, 92, 209, 1)']}
strokeWidth={6}
/>
)}
{[
{latitude: latitude, longitude: longitude, title: 'You'},
{latitude: start.latitude, longitude: start.longitude, title: 'A'},
{latitude: end.latitude, longitude: end.longitude, title: 'B'},
].map((a, i) => (
<Marker
key={i}
draggable
onDragEnd={value => {
if (a.title == 'A') {
setStart({
latitude: value?.nativeEvent.coordinate.latitude,
longitude: value?.nativeEvent.coordinate.longitude,
});
return;
}
if (a.title == 'B') {
setStart({
latitude: value?.nativeEvent.coordinate.latitude,
longitude: value?.nativeEvent.coordinate.longitude,
});
return;
}
}}
pinColor={'rgba(102, 92, 209, 1)'}
title={a.title}
coordinate={{
latitude: a.latitude ?? '',
longitude: a.longitude ?? '',
}}
/>
))}
</MapView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
map: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height - 130,
marginBottom: 20,
},
});