Compare commits

...

2 Commits

Author SHA1 Message Date
a92d0c632f timefix 2025-09-24 13:35:28 +07:00
77e5b345b7 limit osrm request 2025-09-24 13:20:46 +07:00
2 changed files with 92 additions and 72 deletions

View File

@ -88,7 +88,8 @@
route_segment1: "{{ Request::segment(1) }}", route_segment1: "{{ Request::segment(1) }}",
route_segment2: "{{ Request::segment(2) }}", route_segment2: "{{ Request::segment(2) }}",
current_company: 0, // cptid current_company: 0, // cptid
TIMEFIX: new Date().getTimezoneOffset() * -60 // TIMEFIX: new Date().getTimezoneOffset() * -60
TIMEFIX: 25200
} }
// function startTime() { // function startTime() {

View File

@ -164,11 +164,16 @@
// }, // },
// }).addTo(map) // }).addTo(map)
function fetchOsrm(points) { function chunkArray(arr, size) {
// balik koordinat: [lat, lon] -> [lon, lat] const result = [];
const coords = points for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
}
// hints: N-1 semicolon function fetchOsrm(points) {
const coords = points;
const hints = ";".repeat(points.length - 1); const hints = ";".repeat(points.length - 1);
const body = { const body = {
@ -191,14 +196,12 @@
return axios.request(config) return axios.request(config)
.then((response) => { .then((response) => {
// console.log(JSON.stringify(response.data)); return response.data;
return response.data; // supaya bisa dipakai di luar
}) })
.catch((error) => { .catch((error) => {
console.error("Error:", err.message); console.error("Error:", error.message);
return null; return null;
}); });
} }
function decodeOSRMGeometry(encoded) { function decodeOSRMGeometry(encoded) {
@ -233,23 +236,39 @@
return coordinates; return coordinates;
} }
async function getCoordinates(points) {
const chunkSize = 500;
const chunks = chunkArray(points, chunkSize); // Split the points array into chunks of 500
let allCoords = [];
fetchOsrm(points).then(osrm => { for (const chunk of chunks) {
if (!osrm) return console.log("OSRM gagal"); const osrm = await fetchOsrm(chunk); // Fetch OSRM data for each chunk
coords = osrm.routes[0].legs.flatMap(leg => if (!osrm) {
console.log("OSRM failed for chunk");
return;
}
const coords = osrm.routes[0].legs.flatMap(leg =>
leg.steps.flatMap(step => leg.steps.flatMap(step =>
decodeOSRMGeometry(step.geometry) decodeOSRMGeometry(step.geometry)
) )
); );
L.polyline(coords, { allCoords = allCoords.concat(coords); // Combine the result
}
// Now add the polyline to the map
L.polyline(allCoords, {
color: "#2980B9", color: "#2980B9",
weight: 3, weight: 3,
opacity: 0.8 opacity: 0.8
}).addTo(map); }).addTo(map);
// map.fitBounds(coords.map(c => L.latLng(c[0], c[1]))); // 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 // start and finish point