File size: 4,385 Bytes
a3537d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13099cf
 
 
a3537d7
 
13099cf
 
 
a3537d7
13099cf
 
a3537d7
13099cf
a3537d7
 
13099cf
 
a3537d7
13099cf
 
a3537d7
13099cf
a3537d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13099cf
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
// src/utils/routeHelper.js
// Fetches the real driving route between two points and checks how close
// a given point (customer pickup/drop) is to that route.

const OSRM_BASE_URL = process.env.OSRM_BASE_URL || 'https://router.project-osrm.org';
const GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY || '';

function toRad(v) { return (v * Math.PI) / 180; }

function haversineMeters(lat1, lng1, lat2, lng2) {
  const R = 6371000;
  const dLat = toRad(lat2 - lat1);
  const dLng = toRad(lng2 - lng1);
  const a =
    Math.sin(dLat / 2) ** 2 +
    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLng / 2) ** 2;
  return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}

// Decodes a Google/OSRM encoded polyline string into [{lat, lng}, ...]
function decodePolyline(encoded) {
  let index = 0;
  let lat = 0;
  let lng = 0;
  const points = [];
  while (index < encoded.length) {
    let result = 0;
    let shift = 0;
    let b;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    lat += (result & 1) ? ~(result >> 1) : (result >> 1);

    result = 0;
    shift = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    lng += (result & 1) ? ~(result >> 1) : (result >> 1);

    points.push({ lat: lat * 1e-5, lng: lng * 1e-5 });
  }
  return points;
}

// Fetches the real road route between two coordinates.
// Returns an array of {lat, lng} points, or null if it fails.
async function fetchRoutePolyline(startLat, startLng, endLat, endLng) {
  try {
    if (GOOGLE_MAPS_API_KEY) {
      const url = `https://maps.googleapis.com/maps/api/directions/json?origin=${startLat},${startLng}&destination=${endLat},${endLng}&key=${GOOGLE_MAPS_API_KEY}`;
      const res = await fetch(url, { signal: AbortSignal.timeout(6000) });
      const data = await res.json();
      if (data.status === 'OK' && data.routes?.[0]?.overview_polyline?.points) {
        return decodePolyline(data.routes[0].overview_polyline.points);
      }
      console.error('Google Directions error:', data.status);
      return null;
    }

    // Fallback: free public OSRM server, no API key required
    const url = `${OSRM_BASE_URL}/route/v1/driving/${startLng},${startLat};${endLng},${endLat}?overview=full&geometries=polyline`;
    const res = await fetch(url, { signal: AbortSignal.timeout(6000) });
    const data = await res.json();
    if (data.code === 'Ok' && data.routes?.[0]?.geometry) {
      return decodePolyline(data.routes[0].geometry);
    }
    console.error('OSRM error:', data.code);
    return null;
  } catch (err) {
    console.error('fetchRoutePolyline failed:', err.message);
    return null;
  }
}

// Projects a point onto a multi-point road route.
// Returns { distanceMeters, progress } where progress (0..1) tells how far
// along the whole route the closest point is (used to check pickup-before-drop order).
function projectPointOnPolyline(pointLat, pointLng, polyline) {
  if (!Array.isArray(polyline) || polyline.length < 2) return null;

  const segmentLengths = [];
  let totalLength = 0;
  for (let i = 0; i < polyline.length - 1; i++) {
    const segLen = haversineMeters(
      polyline[i].lat, polyline[i].lng,
      polyline[i + 1].lat, polyline[i + 1].lng,
    );
    segmentLengths.push(segLen);
    totalLength += segLen;
  }

  let best = null;
  let runningLength = 0;
  for (let i = 0; i < polyline.length - 1; i++) {
    const a = polyline[i];
    const b = polyline[i + 1];
    const segLen = segmentLengths[i];

    const dx = b.lng - a.lng;
    const dy = b.lat - a.lat;
    const lengthSq = dx * dx + dy * dy;
    let t = 0;
    if (lengthSq > 0) {
      t = ((pointLng - a.lng) * dx + (pointLat - a.lat) * dy) / lengthSq;
      t = Math.max(0, Math.min(1, t));
    }
    const nearLat = a.lat + t * dy;
    const nearLng = a.lng + t * dx;
    const dist = haversineMeters(pointLat, pointLng, nearLat, nearLng);

    if (best === null || dist < best.distanceMeters) {
      best = {
        distanceMeters: dist,
        progress: totalLength > 0 ? (runningLength + segLen * t) / totalLength : 0,
      };
    }
    runningLength += segLen;
  }

  return best;
}

module.exports = {
  fetchRoutePolyline,
  decodePolyline,
  projectPointOnPolyline,
  haversineMeters,
};