From 77e5b345b78cd50c320b557f147c78cf042e7e57 Mon Sep 17 00:00:00 2001 From: Pringgosutono Date: Wed, 24 Sep 2025 13:20:46 +0700 Subject: [PATCH] limit osrm request --- .../menu_v1/reports/_trip_detail.blade.php | 161 ++++++++++-------- 1 file changed, 90 insertions(+), 71 deletions(-) diff --git a/resources/views/menu_v1/reports/_trip_detail.blade.php b/resources/views/menu_v1/reports/_trip_detail.blade.php index 93f298d..5cc09b1 100644 --- a/resources/views/menu_v1/reports/_trip_detail.blade.php +++ b/resources/views/menu_v1/reports/_trip_detail.blade.php @@ -164,92 +164,111 @@ // }, // }).addTo(map) + function chunkArray(arr, size) { + const result = []; + for (let i = 0; i < arr.length; i += size) { + result.push(arr.slice(i, i + size)); + } + return result; + } + function fetchOsrm(points) { - // balik koordinat: [lat, lon] -> [lon, lat] - const coords = points + const coords = points; + const hints = ";".repeat(points.length - 1); - // hints: N-1 semicolon - const hints = ";".repeat(points.length - 1); + const body = { + coordinates: coords, + overview: "false", + alternatives: "false", + steps: "true", + hints: hints + }; - const body = { - coordinates: coords, - overview: "false", - alternatives: "false", - steps: "true", - hints: hints - }; + let config = { + method: 'post', + maxBodyLength: Infinity, + url: 'https://brilianapps.britimorleste.tl/osrm-backend/post-route/v1/driving/', + headers: { + 'Content-Type': 'application/json' + }, + data: body + }; - let config = { - method: 'post', - maxBodyLength: Infinity, - url: 'https://brilianapps.britimorleste.tl/osrm-backend/post-route/v1/driving/', - headers: { - 'Content-Type': 'application/json' - }, - data: body - }; + return axios.request(config) + .then((response) => { + return response.data; + }) + .catch((error) => { + console.error("Error:", error.message); + return null; + }); + } - return axios.request(config) - .then((response) => { - // console.log(JSON.stringify(response.data)); - return response.data; // supaya bisa dipakai di luar - }) - .catch((error) => { - console.error("Error:", err.message); - return null; - }); + function decodeOSRMGeometry(encoded) { + const coordinates = []; + let index = 0, + lat = 0, + lng = 0; - } + while (index < encoded.length) { + let result = 1, + shift = 0, + b; + do { + b = encoded.charCodeAt(index++) - 63 - 1; + result += b << shift; + shift += 5; + } while (b >= 0x1f); + lat += (result & 1 ? ~(result >> 1) : result >> 1); - function decodeOSRMGeometry(encoded) { - const coordinates = []; - let index = 0, - lat = 0, - lng = 0; + result = 1; + shift = 0; + do { + b = encoded.charCodeAt(index++) - 63 - 1; + result += b << shift; + shift += 5; + } while (b >= 0x1f); + lng += (result & 1 ? ~(result >> 1) : result >> 1); - while (index < encoded.length) { - let result = 1, - shift = 0, - b; - do { - b = encoded.charCodeAt(index++) - 63 - 1; - result += b << shift; - shift += 5; - } while (b >= 0x1f); - lat += (result & 1 ? ~(result >> 1) : result >> 1); + coordinates.push([lat / 1e5, lng / 1e5]); + } - result = 1; - shift = 0; - do { - b = encoded.charCodeAt(index++) - 63 - 1; - result += b << shift; - shift += 5; - } while (b >= 0x1f); - lng += (result & 1 ? ~(result >> 1) : result >> 1); + return coordinates; + } - coordinates.push([lat / 1e5, lng / 1e5]); - } + async function getCoordinates(points) { + const chunkSize = 500; + const chunks = chunkArray(points, chunkSize); // Split the points array into chunks of 500 + let allCoords = []; - return coordinates; - } + for (const chunk of chunks) { + const osrm = await fetchOsrm(chunk); // Fetch OSRM data for each chunk + if (!osrm) { + console.log("OSRM failed for chunk"); + return; + } - fetchOsrm(points).then(osrm => { - if (!osrm) return console.log("OSRM gagal"); + const coords = osrm.routes[0].legs.flatMap(leg => + leg.steps.flatMap(step => + decodeOSRMGeometry(step.geometry) + ) + ); - coords = osrm.routes[0].legs.flatMap(leg => - leg.steps.flatMap(step => - decodeOSRMGeometry(step.geometry) - ) - ); + allCoords = allCoords.concat(coords); // Combine the result + } - L.polyline(coords, { - color: "#2980B9", - weight: 3, - opacity: 0.8 - }).addTo(map); - // map.fitBounds(coords.map(c => L.latLng(c[0], c[1]))); - }); + // Now add the polyline to the map + L.polyline(allCoords, { + color: "#2980B9", + weight: 3, + opacity: 0.8 + }).addTo(map); + // map.fitBounds(allCoords.map(c => L.latLng(c[0], c[1]))); + } + + // Usage: Pass the array of coordinates to the function + getCoordinates(points); // start and finish point