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_segment2: "{{ Request::segment(2) }}",
current_company: 0, // cptid
TIMEFIX: new Date().getTimezoneOffset() * -60
// TIMEFIX: new Date().getTimezoneOffset() * -60
TIMEFIX: 25200
}
// function startTime() {

View File

@ -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