Guides
Nearest-driver selection
Assign jobs to the closest available driver by actual drive time using the distance matrix API, with straight-line pre-filtering and batching tips.
The closest driver by straight line is often not the fastest to arrive. Rank candidates by drive time instead.
1. Snap the pickup
Riders drop pins in parks and buildings. Snap the pickup to the road first:
curl "https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862" \
-H "Authorization: Bearer $WAYLINE_API_KEY"const res = await fetch(
"https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862",
{ headers: { Authorization: `Bearer ${process.env.WAYLINE_API_KEY}` } },
);
if (!res.ok) throw new Error(`Wayline ${res.status}: ${await res.text()}`);
const data = await res.json();
console.log(data);import os, requests
res = requests.get(
"https://api.wayline.cloud/v1/nearest",
params={"point": "37.7694,-122.4862"},
headers={"Authorization": f"Bearer {os.environ['WAYLINE_API_KEY']}"},
timeout=10,
)
res.raise_for_status()
print(res.json())package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
req, _ := http.NewRequest("GET", "https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862", nil)
req.Header.Set("Authorization", "Bearer "+os.Getenv("WAYLINE_API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(res.Status, string(body))
}<?php
$ch = curl_init("https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("WAYLINE_API_KEY")],
CURLOPT_TIMEOUT => 10,
]);
$data = json_decode(curl_exec($ch), true);
print_r($data);require "net/http"
require "json"
uri = URI("https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862")
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer #{ENV.fetch("WAYLINE_API_KEY")}"
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(req) }
puts JSON.parse(res.body)import java.net.URI;
import java.net.http.*;
public class Main {
public static void main(String[] args) throws Exception {
var req = HttpRequest.newBuilder(URI.create("https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862"))
.header("Authorization", "Bearer " + System.getenv("WAYLINE_API_KEY"))
.GET().build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.statusCode() + " " + res.body());
}
}import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<void> main() async {
final res = await http.get(
Uri.parse('https://api.wayline.cloud/v1/nearest?point=37.7694,-122.4862'),
headers: {'Authorization': 'Bearer ${Platform.environment['WAYLINE_API_KEY']}'},
);
print(jsonDecode(res.body));
}2. Pre-filter candidates
From your live driver locations, take the ~10–25 nearest by straight line. This keeps the matrix small (and cheap) without missing realistic candidates.
3. Rank by drive time
Send drivers as origins and the snapped pickup as the single destination:
import os, requests
def rank_drivers(drivers, pickup):
res = requests.post(
"https://api.wayline.cloud/v1/matrix",
json={"origins": [{"lat": d["lat"], "lng": d["lng"]} for d in drivers], "destinations": [pickup], "metrics": "duration"},
headers={"Authorization": f"Bearer {os.environ['WAYLINE_API_KEY']}"},
timeout=5,
)
res.raise_for_status()
rows = res.json()["rows"]
ranked = [
(d, row["elements"][0]["duration_seconds"])
for d, row in zip(drivers, rows)
if row["elements"][0]["status"] == "OK"
]
return sorted(ranked, key=lambda x: x[1])4. Dispatch many jobs at once
For batch dispatch, build a drivers × jobs matrix and solve the assignment (e.g. the Hungarian algorithm). Stay within your plan's matrix element limit by tiling.
Related: Rideshare & mobility · Snap to Roads API