-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps-helper-functions.js
132 lines (70 loc) · 2.87 KB
/
maps-helper-functions.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
const directionsAPI = process.env.EXPO_PUBLIC_GOOGLE_API_KEY;
export const generateMarkersFromData = ({ data, userLatitude, userLongitude }) => {
return data.map((driver, index) => {
const latOffset = (index % 5) * 0.002;
const lngOffset = ((index + 1) % 5) * 0.002;
return {
latitude: userLatitude + latOffset,
longitude: userLongitude + lngOffset,
title: `${driver.first_name} ${driver.last_name}`,
...driver,
};
});
};
export const calculateRegion = ({ userLatitude, userLongitude, destinationLatitude, destinationLongitude }) => {
if (!userLatitude || !userLongitude) {
return {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.01,
longitudeDelta: 0.01,
};
}
if (!destinationLatitude || !destinationLongitude) {
return {
latitude: userLatitude,
longitude: userLongitude,
latitudeDelta: 0.01,
longitudeDelta: 0.01,
};
}
const minLat = Math.min(userLatitude, destinationLatitude);
const maxLat = Math.max(userLatitude, destinationLatitude);
const minLng = Math.min(userLongitude, destinationLongitude);
const maxLng = Math.max(userLongitude, destinationLongitude);
const latitudeDelta = (maxLat - minLat) * 1.3;
const longitudeDelta = (maxLng - minLng) * 1.3;
const latitude = (userLatitude + destinationLatitude) / 2;
const longitude = (userLongitude + destinationLongitude) / 2;
return {
latitude,
longitude,
latitudeDelta,
longitudeDelta,
};
};
export const calculateDriverTimes = async ({ markers, userLatitude, userLongitude, destinationLatitude, destinationLongitude }) => {
if ( !userLatitude || !userLongitude || !destinationLatitude || !destinationLongitude ) {
return;
}
try {
const timesPromises = markers.map(async (marker) => {
const responseToUser = await fetch(
`https://maps.googleapis.com/maps/api/directions/json?origin=${marker.latitude},${marker.longitude}&destination=${userLatitude},${userLongitude}&key=${directionsAPI}`
);
const dataToUser = await responseToUser.json();
const timeToUser = dataToUser.routes[0].legs[0].duration.value;
const responseToDestination = await fetch(
`https://maps.googleapis.com/maps/api/directions/json?origin=${userLatitude},${userLongitude}&destination=${destinationLatitude},${destinationLongitude}&key=${directionsAPI}`
);
const dataToDestination = await responseToDestination.json();
const timeToDestination = dataToDestination.routes[0].legs[0].duration.value;
const totalTime = (timeToUser + timeToDestination) / 60;
const price = (totalTime * 0.5).toFixed(2);
return { ...marker, time: totalTime, price };
});
return await Promise.all(timesPromises);
} catch (error) {
console.error('Error calculating driver times:', error);
}
};