limit osrm request
This commit is contained in:
@ -164,92 +164,111 @@
|
|||||||
// },
|
// },
|
||||||
// }).addTo(map)
|
// }).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) {
|
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 body = {
|
||||||
const hints = ";".repeat(points.length - 1);
|
coordinates: coords,
|
||||||
|
overview: "false",
|
||||||
|
alternatives: "false",
|
||||||
|
steps: "true",
|
||||||
|
hints: hints
|
||||||
|
};
|
||||||
|
|
||||||
const body = {
|
let config = {
|
||||||
coordinates: coords,
|
method: 'post',
|
||||||
overview: "false",
|
maxBodyLength: Infinity,
|
||||||
alternatives: "false",
|
url: 'https://brilianapps.britimorleste.tl/osrm-backend/post-route/v1/driving/',
|
||||||
steps: "true",
|
headers: {
|
||||||
hints: hints
|
'Content-Type': 'application/json'
|
||||||
};
|
},
|
||||||
|
data: body
|
||||||
|
};
|
||||||
|
|
||||||
let config = {
|
return axios.request(config)
|
||||||
method: 'post',
|
.then((response) => {
|
||||||
maxBodyLength: Infinity,
|
return response.data;
|
||||||
url: 'https://brilianapps.britimorleste.tl/osrm-backend/post-route/v1/driving/',
|
})
|
||||||
headers: {
|
.catch((error) => {
|
||||||
'Content-Type': 'application/json'
|
console.error("Error:", error.message);
|
||||||
},
|
return null;
|
||||||
data: body
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
return axios.request(config)
|
function decodeOSRMGeometry(encoded) {
|
||||||
.then((response) => {
|
const coordinates = [];
|
||||||
// console.log(JSON.stringify(response.data));
|
let index = 0,
|
||||||
return response.data; // supaya bisa dipakai di luar
|
lat = 0,
|
||||||
})
|
lng = 0;
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error:", err.message);
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
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) {
|
result = 1;
|
||||||
const coordinates = [];
|
shift = 0;
|
||||||
let index = 0,
|
do {
|
||||||
lat = 0,
|
b = encoded.charCodeAt(index++) - 63 - 1;
|
||||||
lng = 0;
|
result += b << shift;
|
||||||
|
shift += 5;
|
||||||
|
} while (b >= 0x1f);
|
||||||
|
lng += (result & 1 ? ~(result >> 1) : result >> 1);
|
||||||
|
|
||||||
while (index < encoded.length) {
|
coordinates.push([lat / 1e5, lng / 1e5]);
|
||||||
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);
|
|
||||||
|
|
||||||
result = 1;
|
return coordinates;
|
||||||
shift = 0;
|
}
|
||||||
do {
|
|
||||||
b = encoded.charCodeAt(index++) - 63 - 1;
|
|
||||||
result += b << shift;
|
|
||||||
shift += 5;
|
|
||||||
} while (b >= 0x1f);
|
|
||||||
lng += (result & 1 ? ~(result >> 1) : result >> 1);
|
|
||||||
|
|
||||||
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 => {
|
const coords = osrm.routes[0].legs.flatMap(leg =>
|
||||||
if (!osrm) return console.log("OSRM gagal");
|
leg.steps.flatMap(step =>
|
||||||
|
decodeOSRMGeometry(step.geometry)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
coords = osrm.routes[0].legs.flatMap(leg =>
|
allCoords = allCoords.concat(coords); // Combine the result
|
||||||
leg.steps.flatMap(step =>
|
}
|
||||||
decodeOSRMGeometry(step.geometry)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
L.polyline(coords, {
|
// Now add the polyline to the map
|
||||||
color: "#2980B9",
|
L.polyline(allCoords, {
|
||||||
weight: 3,
|
color: "#2980B9",
|
||||||
opacity: 0.8
|
weight: 3,
|
||||||
}).addTo(map);
|
opacity: 0.8
|
||||||
// map.fitBounds(coords.map(c => L.latLng(c[0], c[1])));
|
}).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
|
// start and finish point
|
||||||
|
|||||||
Reference in New Issue
Block a user