Call the matrix API
sources are the warehouses, targets are the delivery points; costing sets the travel mode (auto is car).
curl 'https://cloud.gmap.vn/api/tsp/route/sources_to_targets' \
--header 'x-api-key: public-gmap-cloud-x-api-key-demo' \
--header 'content-type: application/json' \
--data '{
"sources": [{ "lat": 21.0285, "lon": 105.8542 }, { "lat": 21.0367, "lon": 105.8352 }],
"targets": [{ "lat": 21.015, "lon": 105.847 }],
"costing": "auto"
}'| Warehouse | Time | Distance |
|---|---|---|
| Hoàn Kiếm (21.0285, 105.8542) | 302 s (~5 min) | 2.733 km |
| Ba Đình (21.0367, 105.8352) | 319 s (~5.3 min) | 3.428 km |
sources_to_targets[i][j] is the result from warehouse i to delivery point j; time is in seconds and distance in the returned units (kilometers).
Pick a warehouse in JavaScript
const warehouses = [
{ name: 'Hoàn Kiếm', lat: 21.0285, lon: 105.8542 },
{ name: 'Ba Đình', lat: 21.0367, lon: 105.8352 },
];
const order = { lat: 21.015, lon: 105.847 };
const response = await fetch(
'https://cloud.gmap.vn/api/tsp/route/sources_to_targets?x-api-key=public-gmap-cloud-x-api-key-demo',
{
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
sources: warehouses.map(({ lat, lon }) => ({ lat, lon })),
targets: [order],
costing: 'auto',
}),
},
);
const { sources_to_targets: rows } = await response.json();
const nearest = rows
.map(([cell], index) => ({ ...warehouses[index], minutes: cell.time / 60, km: cell.distance }))
.sort((a, b) => a.minutes - b.minutes)[0];
// → Hoàn Kiếm, ~5 minutes, 2.733 kmGood to know
- Times are estimates from the routing model and do not include live traffic.
- Batch several orders in one request (several targets) to reduce calls.
- For multi-stop trips, use the stop-ordering API after choosing the warehouse.


