-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (40 loc) · 1.55 KB
/
index.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
import { latLngToCell, gridDistance } from "h3-js";
// Configuration
const resolution = 9; // Define the H3 resolution
const kDistance = 2; // Define the radius of interest (1 = adjacent hexagons)
// Helper function to calculate potential drivers
const findPotentialDrivers = (passengerLat, passengerLng, drivers) => {
// Convert passenger's location to an H3 index
const passengerH3Index = latLngToCell(passengerLat, passengerLng, resolution);
// Initialize result arrays
const potentialDrivers = [];
const nonPotentialDrivers = [];
drivers.forEach((driver) => {
// Convert driver's location to an H3 index
const driverH3Index = latLngToCell(driver.lat, driver.lng, resolution);
// Calculate distance between passenger and driver
const distance = gridDistance(passengerH3Index, driverH3Index);
if (distance !== null && distance <= kDistance) {
potentialDrivers.push(driver);
} else {
nonPotentialDrivers.push(driver);
}
});
return { potentialDrivers, nonPotentialDrivers };
};
// Example usage
const passengerLat = 37.775938728915946;
const passengerLng = -122.41795063018799;
const drivers = [
{ id: "driver1", lat: 37.776, lng: -122.418 },
{ id: "driver2", lat: 37.77, lng: -122.412 },
{ id: "driver3", lat: 37.78, lng: -122.425 },
{ id: "driver4", lat: 37.769, lng: -122.419 },
];
const { potentialDrivers, nonPotentialDrivers } = findPotentialDrivers(
passengerLat,
passengerLng,
drivers
);
console.log("Potential Drivers:", potentialDrivers);
console.log("Non-Potential Drivers:", nonPotentialDrivers);