Tutorial

Add live delivery tracking to a Laravel app

This tutorial adds a “track your order” page to a Laravel shop: the rider app sends its position, Laravel stores it and asks the GeoDesh Routes API for an ETA in traffic, and the customer sees the rider move on a GeoDesh map. GeoDesh is a maps platform from Dhaka that bills in Taka; you need an API key from its console.

30 November 2026 · 9 min read

What we will build

  • A rider_positions table that keeps the latest position of each delivery.
  • An API endpoint the rider app calls every 15 seconds with its coordinates.
  • A service that asks GeoDesh for the route and ETA in traffic, cached for 60 seconds.
  • A tracking page that polls Laravel and moves a marker on a GeoDesh map.

We use polling instead of websockets to keep the tutorial short and the server simple. A 10–15 second refresh is plenty for a delivery on a city street.

1. Configuration

Store the key in .env and expose it through config/services.php. The key stays on the server for routing calls. For the map in the browser, create a second key in the GeoDesh console restricted to your site’s origin and to the maps scope.

# .env
GEODESH_KEY=your_server_key
GEODESH_BROWSER_KEY=your_origin_restricted_key

// config/services.php
'geodesh' => [
    'key' => env('GEODESH_KEY'),
    'browser_key' => env('GEODESH_BROWSER_KEY'),
],
.env and config/services.php

2. The table

Schema::create('rider_positions', function (Blueprint $table) {
    $table->id();
    $table->foreignId('order_id')->unique()->constrained();
    $table->decimal('lat', 9, 6);
    $table->decimal('lng', 9, 6);
    $table->timestamp('reported_at');
    $table->timestamps();
});
database/migrations/xxxx_create_rider_positions_table.php

One row per order is enough: we only need the latest position. If you want a trail for disputes, add a second history table later.

3. Receiving positions from the rider app

Route::middleware('auth:sanctum')->post('/orders/{order}/position', function (Request $request, Order $order) {
    abort_unless($request->user()->id === $order->rider_id, 403);
    $data = $request->validate([
        'lat' => ['required', 'numeric', 'between:-90,90'],
        'lng' => ['required', 'numeric', 'between:-180,180'],
    ]);
    RiderPosition::updateOrCreate(
        ['order_id' => $order->id],
        [...$data, 'reported_at' => now()],
    );
    return response()->noContent();
});
routes/api.php

The check that the signed-in rider owns the order matters: without it, anyone with a token could move any rider on the map.

4. ETA from the GeoDesh Routes API

POST /v1/routes takes an origin and destination as coordinates or addresses and returns duration, durationInTraffic, distance and an encoded polyline. We cache each order’s answer for 60 seconds so a customer refreshing the page does not trigger a new paid call each time.

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class GeoDesh
{
    public function eta(int $orderId, array $from, array $to): ?array
    {
        return Cache::remember("eta:$orderId", 60, function () use ($from, $to) {
            $res = Http::withHeaders(['x-api-key' => config('services.geodesh.key')])
                ->timeout(10)
                ->post('https://geodesh.com/v1/routes', [
                    'origin' => ['lat' => (float) $from['lat'], 'lng' => (float) $from['lng']],
                    'destination' => ['lat' => (float) $to['lat'], 'lng' => (float) $to['lng']],
                    'mode' => 'driving',
                ]);
            if (! $res->ok()) {
                return null;
            }
            $d = $res->json('data');
            return [
                'seconds' => $d['durationInTraffic']['value'] ?? $d['duration']['value'],
                'text' => $d['durationInTraffic']['text'] ?? $d['duration']['text'],
                'metres' => $d['distance']['value'],
                'polyline' => $d['polyline'],
            ];
        });
    }
}
app/Services/GeoDesh.php

5. The tracking endpoint

Route::get('/track/{order:tracking_code}/status', function (Order $order, GeoDesh $geo) {
    $pos = RiderPosition::where('order_id', $order->id)->first();
    if (! $pos) {
        return ['state' => 'waiting'];
    }
    $eta = $geo->eta($order->id, $pos->only('lat', 'lng'), ['lat' => $order->lat, 'lng' => $order->lng]);
    return [
        'state' => 'on_the_way',
        'rider' => ['lat' => (float) $pos->lat, 'lng' => (float) $pos->lng],
        'updated' => $pos->reported_at->diffForHumans(),
        'eta' => $eta,
    ];
});
routes/web.php

Use a random tracking_code in the URL instead of the order id, so customers cannot guess other orders.

6. The customer page with a live map

The GeoDesh Map SDK is one script. addMarker returns a marker with setLatLng(), and addPolyline accepts the encoded polyline from the Routes API directly.

<div id="map" style="height:420px"></div>
<p id="eta">Waiting for the rider…</p>
<script src="https://geodesh.com/sdk/atlas-map.js?v=1.2.1"></script>
<script>
  const home = { lat: {{ $order->lat }}, lng: {{ $order->lng }} };
  const map = new AtlasMap('#map', { apiKey: '{{ config('services.geodesh.browser_key') }}', center: [home.lat, home.lng], zoom: 14 });
  map.addMarker({ ...home, title: 'Delivery address', kind: 'pin' });
  let rider = null, line = null;

  async function refresh() {
    const s = await fetch('/track/{{ $order->tracking_code }}/status').then((r) => r.json());
    if (s.state !== 'on_the_way') return;
    if (!rider) rider = map.addMarker({ ...s.rider, title: 'Rider', kind: 'dot', pulse: true });
    else rider.setLatLng(s.rider);
    if (s.eta) {
      document.getElementById('eta').textContent = 'Arriving in about ' + s.eta.text;
      line?.remove();
      line = map.addPolyline(s.eta.polyline, { color: '#4f46e5', width: 4 });
    }
  }
  refresh();
  setInterval(refresh, 15000);
</script>
resources/views/track.blade.php

7. What it costs

Each refresh reads the cached ETA, so the paid call happens at most once a minute per active order. A 25-minute delivery makes about 25 directions calls. At 1,000 deliveries a day that is about 750,000 calls a month, ৳187,500 at the list price of ৳250 per 1,000, before plan discounts. To cut it, refresh the ETA every two or three minutes instead; riders do not change ETA much minute to minute. Map loads cost ৳250 per 1,000 and are counted once per page view.

Production checklist

  • Restrict the browser key to your site’s origin and the maps scope in the GeoDesh console.
  • Keep the server key in .env only, never in JavaScript.
  • Stop accepting positions once the order is delivered.
  • Delete old positions after the dispute window your business needs.
  • Watch usage in the console and set your plan quota as a ceiling.

Next steps: assign riders by travel time with the distance matrix, or plan multi-stop runs with route optimisation. All prices are on the pricing page.

Privacy for riders and customers

A live position is personal data. Only show the rider while the order is on its way, hide the exact position once the rider is within a short distance of the door if your riders prefer, and delete positions after the period your business needs for disputes. Tell riders in their app when tracking is on.

Common pitfalls

  • Battery drain: sending a position every second empties a rider’s phone. Every 10–15 seconds while moving is enough; slow down when the rider stops.
  • GPS jumps: indoor or tall-building GPS can jump hundreds of metres. Ignore a new point if it implies an impossible speed since the last one.
  • Stale positions: show “last updated 2 minutes ago” so customers know when the rider’s phone has lost signal.

Frequently asked questions

Can I use websockets instead of polling?

Yes. Broadcast a RiderMoved event with Laravel Echo and update the marker on the event. Keep the ETA calculation on the server and cached either way.

Which key goes in the browser?

A separate key restricted to your site’s origin and the maps scope. The routing key stays in .env on the server.

How do I draw the route the rider will take?

Pass the polyline from POST /v1/routes to map.addPolyline(); the SDK decodes it for you.

Start with ৳5,000 free every month.

Request an account and the GeoDesh team sets it up for your organization. Then make a key and your first call in minutes. Billed in Taka.