Turn-by-turn instructions
List each step of the route and fly to it when clicked.
Code
Replace YOUR_API_KEY with a key from your Gebeta dashboard, save the file as index.html and open it in a browser.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Turn-by-turn instructions</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Geist:wght@400;500&display=swap" />
<script src="https://tiles.gebeta.app/static/v3/gebeta-maps.umd.js"></script>
<style>
body { margin: 0; font: 13px/1.4 Geist, system-ui, sans-serif; color: #161616; }
#map { position: absolute; inset: 0; }
.card { position: absolute; top: 12px; left: 12px; z-index: 1; max-width: calc(100% - 52px); padding: 10px 14px; border-radius: 8px; background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.07); }
.card strong { display: block; font-weight: 500; }
.card small { color: #8a8a8a; font-size: 13px; }
.pin { width: 28px; height: 45px; filter: drop-shadow(0 0 1px #fff) drop-shadow(0 0 1px #fff); }
.pin span { position: absolute; top: -4px; left: 20px; min-width: 16px; height: 16px; padding: 0 4px; box-sizing: border-box; border: 1px solid #fff; border-radius: 999px; background: #191715; color: #faf9f7; font: 500 10px/14px Geist, system-ui, sans-serif; text-align: center; }
.label { margin-bottom: 6px; padding: 6px 10px; border-radius: 999px; background: #191715; color: #faf9f7; font: 500 13px/1 Geist, system-ui, sans-serif; white-space: nowrap; pointer-events: none; }
.label::after { content: ""; position: absolute; top: 100%; left: 50%; transform: translateX(-50%); border: solid transparent; border-width: 6px 5px 0; border-top-color: #191715; }
.steps { position: absolute; top: 12px; left: 12px; bottom: 12px; z-index: 1; display: flex; flex-direction: column; width: 288px; max-width: calc(100% - 64px); overflow: hidden; border-radius: 8px; background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.07); }
.steps header { padding: 12px 16px; border-bottom: 1px solid #ececec; }
.steps header strong { display: block; font-size: 14px; font-weight: 500; }
.steps header small { color: #8a8a8a; font-size: 13px; }
.steps ol { margin: 0; padding: 0; overflow-y: auto; list-style: none; }
.steps li button { display: flex; gap: 12px; width: 100%; padding: 10px 16px; border: 0; border-bottom: 1px solid #ececec; border-radius: 0; background: none; box-shadow: none; text-align: left; font: 14px/1.4 Geist, system-ui, sans-serif; color: #161616; cursor: pointer; }
.steps li button:hover { background: rgb(0 0 0 / 0.04); }
.steps li button[aria-pressed="true"] { background: #f4f4f4; }
.steps li small { display: block; color: #8a8a8a; font-size: 13px; }
.number { display: grid; flex-shrink: 0; place-items: center; width: 20px; height: 20px; border-radius: 999px; background: #f4f4f4; color: #525252; font: 500 11px/1 Geist, system-ui, sans-serif; }
[aria-pressed="true"] .number, .step-marker.active { background: #ffa600; color: #1a1305; }
@media (max-width: 560px) { .steps { top: auto; right: 12px; width: auto; max-width: none; height: 42%; } }
.step-marker { display: grid; place-items: center; width: 20px; height: 20px; border: 2px solid #fff; border-radius: 999px; background: #191715; color: #faf9f7; font: 500 10px/1 Geist, system-ui, sans-serif; cursor: pointer; }
</style>
</head>
<body>
<div id="map"></div>
<div class="steps">
<header><strong>Turn-by-turn</strong><small>Finding a route…</small></header>
<ol></ol>
</div>
<script>
const INK = "#191715";
function pin(color, label) {
const element = document.createElement("div");
element.className = "pin";
element.innerHTML = `<svg width="28" height="45" viewBox="0 0 42 68"><path fill="${color}" d="M21 0a21 21 0 0 1 2 41.9V64a2 2 0 0 1-4 0V41.9A21 21 0 0 1 21 0Z"/><circle cx="21" cy="21" r="7" fill="#fff"/></svg>`;
if (label) element.insertAdjacentHTML("beforeend", `<span>${label}</span>`);
return element;
}
const origin = { lat: 8.989022, lng: 38.79036 };
const destination = { lat: 9.03045, lng: 38.7653 };
const { LngLatBounds, Marker } = window.gebeta;
const gebeta = new GebetaMaps({ apiKey: "YOUR_API_KEY" });
const map = gebeta.init({
container: "map",
center: [38.7785, 9.0098],
zoom: 12.5,
});
const ROUTE_STYLE = { "line-color": INK, "line-width": 5, "line-opacity": 1 };
function addCasing() {
if (map.getLayer("route-casing")) return;
map.addLayer(
{
id: "route-casing",
type: "line",
source: "gebeta-route",
layout: { "line-join": "round", "line-cap": "round" },
paint: { "line-color": "#fff", "line-width": 8 },
},
"gebeta-route"
);
}
function halfway(coordinates) {
const steps = coordinates.slice(1).map((point, i) => Math.hypot(point[0] - coordinates[i][0], point[1] - coordinates[i][1]));
let remaining = steps.reduce((sum, step) => sum + step, 0) / 2;
for (let i = 0; i < steps.length; i++) {
if (remaining <= steps[i]) return coordinates[i + 1];
remaining -= steps[i];
}
return coordinates[coordinates.length - 1];
}
function durationLabel(route) {
const element = document.createElement("div");
element.className = "label";
element.textContent = route.duration;
return new Marker({ element, anchor: "bottom" }).setLngLat(halfway(route.geometry.coordinates));
}
function fitRoute(route, left = 64) {
const bounds = new LngLatBounds();
route.geometry.coordinates.forEach((coordinate) => bounds.extend(coordinate));
map.fitBounds(bounds, { padding: { top: 96, bottom: 48, left, right: 64 }, maxZoom: 16, duration: 800 });
}
const list = document.querySelector(".steps ol");
const markers = [];
function select(index, step) {
list.querySelectorAll("button").forEach((button, i) => button.setAttribute("aria-pressed", String(i === index)));
markers.forEach((marker, i) => marker.getElement().classList.toggle("active", i === index));
map.flyTo({ center: step.coord, zoom: 17, speed: 1.5 });
}
map.on("load", async () => {
const route = await gebeta.getDirections(origin, destination);
gebeta.displayRoute(route, { showMarkers: false, routeStyle: ROUTE_STYLE });
addCasing();
fitRoute(route, innerWidth > 560 ? 330 : 64);
new Marker({ element: pin(INK, "A"), anchor: "bottom" }).setLngLat(origin).addTo(map);
new Marker({ element: pin(INK, "B"), anchor: "bottom" }).setLngLat(destination).addTo(map);
document.querySelector(".steps small").textContent = `${route.instructions.length} steps · ${route.duration}`;
route.instructions.forEach((step, index) => {
const element = document.createElement("div");
element.className = "step-marker";
element.textContent = index + 1;
element.addEventListener("click", () => select(index, step));
markers.push(new Marker({ element }).setLngLat(step.coord).addTo(map));
const item = document.createElement("li");
item.innerHTML = `<button><span class="number">${index + 1}</span><span>${step.instruction}<small>${Math.round(step.length * 1000)} m</small></span></button>`;
item.querySelector("button").addEventListener("click", () => select(index, step));
list.append(item);
});
});
</script>
</body>
</html>