Directions
milsat.directions(origin, destination, { mode }) finds a route by road. mode is "driving" (the default) or "walking".
Live: this is the code below, running.
const map = new milsat.Map(document.getElementById("map"), {
apiKey: "YOUR_API_KEY",
center: [3.38, 6.52],
zoom: 11,
});
const from = { lat: 6.6018, lng: 3.3515 }; // Ikeja
const to = { lat: 6.4281, lng: 3.4219 }; // Victoria Island
const route = await milsat.directions(from, to, { mode: "driving" });
new milsat.Polyline({ path: route.path, map, strokeColor: "#2563eb", strokeWidth: 5 });
new milsat.Marker({ position: from, map, color: "#16a34a" });
new milsat.Marker({ position: to, map, color: "#dc2626" });
map.fitBounds([[3.33, 6.42], [3.44, 6.61]], { padding: 30 });
document.getElementById("output").textContent =
`${(route.distance / 1000).toFixed(1)} km, ${Math.round(route.duration / 60)} min\n\n` +
route.steps.map((step) => `${step.instruction} (${Math.round(step.distance)} m)`).join("\n");The route
| Field | |
|---|---|
distance | Metres. |
duration | Seconds, without traffic. |
path | The line as { lng, lat } positions, ready for a Polyline. |
geometry | The same line as a GeoJSON LineString. |
steps | The turn list: instruction, name, distance, duration, location, and maneuver for an icon (left, slight_right, roundabout, arrive…). |
When there is no route
The promise rejects with a MilsatError whose code is no_route, for example when one end is on an island with no road to it.
Times assume free-flowing traffic. Show them as an estimate.