إسناد التوصيل: اختيار أقرب مندوب
Compare several riders against one restaurant with Distance Matrix, handle unreachable cells, and understand what the request costs.
The naive dispatch loop asks for a route from every rider to the restaurant, one request each, and sorts by duration. Distance Matrix does the same work in one request and one charge per cell, and it tells you explicitly which cells the road network cannot reach.
The shape of the request
- origins are the riders' current positions: one row each.
- destinations is the restaurant: one column. Put the restaurant in destinations, not in origins, so the durations read rider to restaurant.
- The response is row-major: durations[i][j] is the cost from origins[i] to destinations[j].
- A cell the road network cannot reach is null, and the same index appears in errors. It is not a zero and it is not a large number.
const riders = [
{ id: "r-1", latitude: 30.0444, longitude: 31.2357 },
{ id: "r-2", latitude: 30.0626, longitude: 31.2497 },
{ id: "r-3", latitude: 30.0131, longitude: 31.2089 },
];
const matrix = await fetch("https://api.bardiyamap.com/v1/distance-matrix", {
method: "POST",
headers: { "X-API-Key": key, "Content-Type": "application/json" },
body: JSON.stringify({
origins: riders.map(({ latitude, longitude }) => ({ latitude, longitude })),
destinations: [{ latitude: 30.0511, longitude: 31.2401 }],
profile: "driving",
}),
}).then((response) => response.json());
const candidates = riders
.map((rider, index) => ({
rider,
duration: matrix.durations?.[index]?.[0] ?? null,
distance: matrix.distances?.[index]?.[0] ?? null,
}))
.filter((candidate) => candidate.duration !== null) // drop unreachable riders
.sort((a, b) => a.duration! - b.duration!);
const winner = candidates[0];Batching above the cap
The service accepts at most 25 origins, 25 destinations and 625 cells; a larger request returns 413. A fleet of 60 riders needs three requests of 25, not one request of 60.
def rank_riders(riders, restaurant, chunk=25):
ranked=[]
for start in range(0, len(riders), chunk):
batch = riders[start:start+chunk]
matrix = post_matrix(batch, [restaurant])
for i, rider in enumerate(batch):
duration = matrix["durations"][i][0]
if duration is not None:
ranked.append((duration, rider))
ranked.sort(key=lambda pair: pair[0])
return rankedBilling is per cell, so the chunked approach costs exactly what the single oversized request would have: origins multiplied by destinations. Chunking is not a cost optimisation, it is a correctness requirement.
When to use directions instead
- Matrix answers how long, not how. If the dispatch screen must draw the winning route, call POST /v1/directions once for the winner only.
- Matrix does not return geometry, steps or alternatives. Requesting those is a separate call.
- One rider, one restaurant, and you need the route anyway: skip the matrix and call directions directly. It is one charge either way.
Read the freshness of the answer
A matrix is a snapshot of the road network at request time, not a prediction. Re-request when a rider moves materially, and cache the winner for the few seconds it takes to send the offer rather than re-running dispatch on every position update.
