-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapboxshowAdress
More file actions
394 lines (338 loc) · 11 KB
/
mapboxshowAdress
File metadata and controls
394 lines (338 loc) · 11 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
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
import React, { useEffect, useRef, useState } from 'react';
import { View, StyleSheet, Dimensions } from 'react-native';
import MapboxGL, { Logger } from '@rnmapbox/maps';
import { MD3LightTheme, IconButton, Text } from '@jmstechnologiesinc/react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import { Config } from '../Config'
import Mapbox from '@rnmapbox/maps';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import VehiclesList from './VehiclesList';
import { moderateScale } from '@jmstechnologiesinc/react-native-size-matters'
Logger.setLogCallback((log) => {
const { message } = log;
if (
message.match('Request failed due to a permanent error: Canceled') ||
message.match('Request failed due to a permanent error: Socket Closed')
) {
return true;
}
return false;
});
const APIKEY = Config.MAPBOX_ACCESS_TOKEN;
MapboxGL.setAccessToken(APIKEY);
MapboxGL.setTelemetryEnabled(false);
const GeoPositionTracker = ({
customerPosition,
currentDriverPosition,
vendorPosition,
currentSnapPoint,
nearbyVehicleLocations,
getGPSLocationOnPress,
isLocationPermission
}) => {
const mapRef = useRef(null);
const [routeDirections, setRouteDirections] = useState(null);
const [destinationCoords, setDestinationCoords] = useState([
customerPosition?.longitude,
customerPosition?.latitude,
]);
const insets = useSafeAreaInsets();
const top = insets.top === 0 ? MD3LightTheme.spacing.x8 : insets.top;
const right = insets.right === 0 ? MD3LightTheme.spacing.x8 : insets.right;
const left = insets.left === 0 ? MD3LightTheme.spacing.x8 : insets.left
const [driverHeading, setDriverHeading] = useState(0)
const [zoomLevel, setZoomLevel] = useState(12);
const [boundingBox, setBoundingBox] = useState(null);
const { height } = Dimensions.get('window');
const filterVehiclePositions = nearbyVehicleLocations?.filter(item =>
!(item.latitud === currentDriverPosition?.latitude && item.longitud === currentDriverPosition?.longitude)
);
const getBoundingBox = (coordinates) => {
let minLng = Infinity;
let minLat = Infinity;
let maxLng = -Infinity;
let maxLat = -Infinity;
coordinates.forEach(coord => {
const [lng, lat] = coord;
minLng = Math.min(minLng, lng);
minLat = Math.min(minLat, lat);
maxLng = Math.max(maxLng, lng);
maxLat = Math.max(maxLat, lat);
});
return {
sw: [minLng, minLat],
ne: [maxLng, maxLat],
}
};
const calculateZoomLevel = (boundingBox) => {
const width = boundingBox.ne[0] - boundingBox.sw[0];
const height = boundingBox.ne[1] - boundingBox.sw[1];
const area = width * height;
if (area < 0.01) return 14
if (area < 0.1) return 12
return 10;
};
const createRouteLine = async (startPosition, endPosition) => {
const startCoords = `${startPosition.longitude},${startPosition.latitude}`;
const endCoords = `${endPosition.longitude},${endPosition.latitude}`;
const geometries = 'geojson';
const typeVehicle = 'driving';
const url = `https://api.mapbox.com/directions/v5/mapbox/${typeVehicle}/${startCoords};${endCoords}?alternatives=false&geometries=${geometries}&steps=true&overview=full&access_token=${APIKEY}`;
try {
const response = await fetch(url);
const json = await response.json();
if (json.routes && json.routes.length) {
const route = json.routes[0];
const coordinates = route.geometry.coordinates;
const steps = route.legs[0]?.steps;
if (steps && steps.length) {
const heading = steps[0].maneuver.bearing_after;
setDriverHeading(heading);
}
setRouteDirections(makeRouterFeature(coordinates));
setDestinationCoords(coordinates[coordinates.length - 1]);
const boundingBox = getBoundingBox(coordinates);
const calculatedZoomLevel = calculateZoomLevel(boundingBox);
setBoundingBox(boundingBox);
setZoomLevel(calculatedZoomLevel);
}
} catch (error) {
console.error('Error fetching directions:', error);
}
};
useEffect(() => {
createRouteLine(vendorPosition, customerPosition);
}, []);
useEffect(() => {
if (currentDriverPosition) {
createRouteLine(currentDriverPosition, customerPosition);
}
}, [currentDriverPosition]);
useEffect(() => {
if (currentSnapPoint === 0.8) {
mapRef.current?.setCamera({
bounds: boundingBox,
zoomLevel: zoomLevel,
padding: {
paddingTop: top,
paddingRight: right,
paddingLeft: left,
paddingBottom: height * currentSnapPoint,
},
animationMode: 'flyTo',
animationDuration: 250,
})
} else {
mapRef.current?.setCamera({
bounds: boundingBox,
zoomLevel: zoomLevel,
padding: {
paddingTop: top,
paddingRight: right,
paddingLeft: left,
paddingBottom: height * currentSnapPoint,
},
animationMode: 'flyTo',
animationDuration: 250,
})
}
}, [currentSnapPoint]);
const makeRouterFeature = (coordinates) => {
return {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: coordinates,
},
},
],
};
};
const centerCoordinate = currentDriverPosition
? [currentDriverPosition?.longitude, currentDriverPosition?.latitude]
: vendorPosition
? [vendorPosition?.longitude, vendorPosition?.latitude]
: false;
const resetToInitialPosition = async () => {
if (!isLocationPermission) {
await getGPSLocationOnPress();
}
if (mapRef.current) {
mapRef.current.setCamera({
centerCoordinate: [customerPosition?.longitude, customerPosition?.latitude],
zoomLevel: zoomLevel,
bounds: boundingBox,
padding: {
paddingTop: top,
paddingRight: right,
paddingLeft: left,
paddingBottom: height * currentSnapPoint,
},
animationMode: 'flyTo',
animationDuration: 500,
});
}
};
return (
<MapboxGL.MapView
style={{
flex: 1,
}}
zoomEnabled={true}
styleURL={Mapbox.StyleURL.Street}
compassEnabled={false}
logoEnabled={false}
attributionEnabled={false}
scaleBarEnabled={false}
mapRef={mapRef}
>
<MapboxGL.Camera
zoomLevel={zoomLevel}
bounds={boundingBox}
ref={mapRef}
padding={{
paddingTop: 100,
paddingRight: right,
paddingLeft: left,
paddingBottom: height * currentSnapPoint,
}}
animationMode="flyTo"
animationDuration={200}
/>
{centerCoordinate && customerPosition && vendorPosition ?
<MapboxGL.ShapeSource id="routeSource" shape={routeDirections}>
<MapboxGL.LineLayer id="routeLine" style={{ lineColor: MD3LightTheme.colors.primary, lineWidth: 4 }} />
</MapboxGL.ShapeSource>
:
<MapboxGL.UserLocation animated={true} androidRenderMode="gps" showsUserHeadingIndicator={true} />
}
{centerCoordinate && customerPosition ? (
<>
<MapboxGL.Images images={{ driverIcon: require('./tracking/car.png') }} />
<MapboxGL.ShapeSource id="driverSource" shape={routeDirections}>
<MapboxGL.SymbolLayer
id="driverIconLayer"
style={{
iconImage: 'driverIcon',
iconAnchor: 'center',
iconAllowOverlap: true,
iconRotate: driverHeading,
iconSize: 0.5,
}}
/>
</MapboxGL.ShapeSource>
</>
) : null}
{
centerCoordinate && customerPosition && vendorPosition ?
<MapboxGL.PointAnnotation id="destination" coordinate={destinationCoords}>
<View style={styles.destinationIcon}>
<MaterialCommunityIcons name="map-marker-radius" size={24} color={MD3LightTheme.colors.primary} />
</View>
</MapboxGL.PointAnnotation>
:
null
}
{nearbyVehicleLocations &&
<VehiclesList
vehicleListPositions={nearbyVehicleLocations}
filterVehiclePositions={filterVehiclePositions}
driverHeading={driverHeading}
/>
}
{
destinationCoords &&
destinationCoords[0] !== undefined &&
destinationCoords[1] !== undefined ?
<MapboxGL.MarkerView
id="marker"
coordinate={destinationCoords}
anchor={{ x: 0.5, y: 1 }}
// anchor={{ x: 0.5, y: -1 }}
style={{
paddingRight: insets.right,
// marginVertical: 100
}}
>
<View style={{
backgroundColor: 'white',
paddingHorizontal: 10,
paddingVertical: 4,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
maxWidth: 200,
}} >
<Text style={{
color: '#000',
fontSize: 14,
marginRight: 5,
}}>Hata Mayor Santiago</Text>
<MaterialCommunityIcons name="chevron-right" size={24} color="black" />
</View>
</MapboxGL.MarkerView>
:
null
}
{
centerCoordinate &&
centerCoordinate[0] !== undefined &&
centerCoordinate[1] !== undefined ?
<MapboxGL.MarkerView
id="marker"
coordinate={centerCoordinate}
// anchor={{ x: 0.5, y: -1 }}
anchor={{ x: 0.5, y: 1 }}
style={{
paddingLeft: insets.left,
}}
>
<View style={{
backgroundColor: 'white',
paddingHorizontal: 10,
paddingVertical: 4,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
maxWidth: 200,
}} >
<Text style={{
color: '#000',
fontSize: 14,
marginRight: 5,
}}>Hata Mayor Santiago</Text>
<MaterialCommunityIcons name="chevron-right" size={24} color="black" />
</View>
</MapboxGL.MarkerView>
:
null
}
<View style={{
position: 'absolute',
bottom: height * 0.48,
right: 0,
}}>
<IconButton
icon="crosshairs-gps"
size={moderateScale(24)}
mode='contained'
onPress={resetToInitialPosition}
/>
</View>
</MapboxGL.MapView>
);
};
const styles = StyleSheet.create({
destinationIcon: {
flex: 1,
width: 30,
height: 30,
justifyContent: 'center',
alignItems: 'center',
},
});
export default GeoPositionTracker;