Routes
Overview
The <x-map-route> component draws polylines on the map. It can also fetch real driving, walking, or cycling directions from an OSRM routing service, animate the route drawing, show how far along the route a trip has travelled, and pin markers to the route with <x-map-route-marker>. Routes react to Livewire state: change the bound props and the route updates in place.
Basic Polyline
Draw a simple line connecting a series of coordinates:
Route with Stops
Draw a dot at each waypoint you passed in :coordinates using the :with-stops prop. When directions are fetched, dots are only drawn at your waypoints, not at every vertex of the returned road geometry:
OSRM Directions
Fetch real road geometry from an OSRM server for driving, walking, or cycling directions:
Animated Route
Animate the route as it draws on the map. The line grows smoothly by distance over :animate-duration milliseconds:
<x-map-route :coordinates="$coords" :fetch-directions="true" directions-profile="driving" :animate="true" color="#06B6D4" />
Route Progress
Progress (0 to 1) draws a "travelled" line on top of the route, which is useful for deliveries, trips and playback. Progress usually changes many times a second (a slider, playback, live tracking), so drive it on the client: point progress-model at an Alpine property and the route follows it with no server request. Use $wire.entangle() (deferred) if your component needs the value; it is sent along with the next request instead of on every change. Drag the slider:
The progress line inherits the route's color and width unless you set progress-color, :progress-width, :progress-opacity or :progress-dash-array. Other ways to set it: :progress="$progress" renders it from the server (fine for values that change occasionally, e.g. on wire:poll; add :progress-duration to tween), $mapRoute('id').setProgress(0.75, 1000) from any Alpine expression, or from PHP and plain JavaScript:
// From Livewire: animate to 75% over one second $this->dispatch('map:route-progress-progress-route', progress: 0.75, duration: 1000); // From JavaScript LivewireMap.getRoute('progress-route').setProgress(0.75, 1000);
Route Markers
<x-map-route-marker> pins a marker to its parent <x-map-route>. It stays on the route when directions load, when you switch alternatives, and as progress changes. Set at to start, end, progress (follows :progress) or a fraction such as :at="0.5" for the midpoint. :align-to-route="true" rotates the marker to the route heading, so design custom content pointing up (north).
Route markers accept the same children as <x-map-marker>: <x-marker-content>, <x-marker-tooltip>, <x-marker-popup> and <x-marker-label>. To place one outside the route, pass route-id.
| Prop | Type | Default | Description |
|---|---|---|---|
| at | string|float | 'progress' | start, end, progress or a 0–1 fraction along the route |
| :align-to-route | bool | false | Rotate the marker to the route heading at its position |
| route-id | ?string | null | Route to attach to when not nested inside <x-map-route> |
| id | ?string | uuid | Marker id used in marker events |
| color | string | '#1A56DB' | Color of the default dot |
| anchor | string | 'center' | Which part of the marker sits on the route |
| :offset | array | [0, 0] | Pixel offset [x, y] |
| :rotation | float | 0 | Rotation in degrees (added to the heading when aligned) |
| rotation-alignment | string | 'auto' | map, viewport or auto |
| pitch-alignment | string | 'auto' | map, viewport or auto |
| class | string | '' | Additional CSS classes |
<x-map-route-group>
Render an array of routes where one can be "active" or "selected":
| Prop | Description |
|---|---|
| :routes | Array of route objects with color, coordinates, etc. |
| id | Unique identifier |
| :selected-route | Index or ID of the initially selected route (Default: 0) |
| :fit-bounds | Auto-fit map bounds to show all routes (Default: false) |
| :fetch-directions | Fetch directions for all routes (Default: false) |
| :animate | Animate route drawing (Default: false) |
| class | Wrapper container classes |
<x-map-route-list>
Overlay UI panel displaying route alternatives and details:
| Prop | Description |
|---|---|
| route-id | ID of the x-map-route to link to |
| map-id | Deprecated: the list now finds its route by route-id anywhere on the page |
| :show-distance | Show total distance (Default: true) |
| :show-duration | Show travel time (Default: true) |
| :show-fastest-badge | Highlight fastest route (Default: true) |
| :show-time-diff | Show time difference between alternatives (Default: true) |
| position | Options: top-left, top-right, etc. (Default: 'top-left') |
| title | Panel header text (Default: 'Routes') |
| width | Extra width class for the panel, e.g. w-60 (Default: '', the package CSS sets a sensible width) |
| container-class | Extra classes for the wrapper container |
| header-class | Extra classes for the header section |
| item-class | Extra classes for individual route items |
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| :coordinates | array | required | Array of [lng, lat] pairs |
| id | ?string | null | Unique identifier for the route |
| color | string | '#1A56DB' | Route line color |
| :width | int|float | 4 | Pixel width |
| :opacity | float | 1.0 | Line opacity (0.0-1.0) |
| :dash-array | ?array | null | Dash pattern array |
| line-cap | string | 'round' | Line cap style |
| line-join | string | 'round' | Line join style |
| active-color | string | '#0F43C4' | Color when active/selected |
| :active-width | int | 6 | Width when active/selected |
| :active-opacity | ?float | null | Opacity when active (falls back to :opacity) |
| :active-dash-array | ?array | null | Dash pattern when active (falls back to :dash-array) |
| hover-color | ?string | null | Color on hover |
| :clickable | bool | true | Enable click interactions |
| :with-stops | bool | false | Draw a dot at each waypoint passed in :coordinates |
| stop-color | string | '#1A56DB' | Color for stop markers |
| :fetch-directions | bool | false | Auto-fetch geom from OSRM |
| directions-profile | string | 'driving' | driving, walking, cycling |
| directions-url | ?string | null | Custom OSRM server URL |
| :animate | bool | false | Animate the line growing by distance |
| :animate-duration | int | 2000 | Animation duration (ms) |
| :active | bool | false | Set as initially active |
| :alternatives | bool | false | (OSRM) Fetch alternative routes |
| :max-alternatives | int | 2 | Max alternatives to fetch |
| alternative-color | string | '#94A3B8' | Color for alternatives |
| :alternative-opacity | float | 0.5 | Opacity for alternatives |
| :alternative-width | int | 3 | Width for alternatives |
| :progress | ?float | null | 0–1. Draws a "travelled" line on top of the route |
| progress-color | ?string | null | Progress line color (defaults to the route color) |
| :progress-width | int|float|null | null | Progress line width (defaults to the route width) |
| :progress-opacity | ?float | null | Progress line opacity (defaults to 1) |
| :progress-dash-array | ?array | null | Progress line dash pattern (solid by default) |
| progress-model | ?string | null | Alpine expression the progress follows on the client (no server request per change), e.g. progress from x-data="{ progress: $wire.entangle('progress') }" |
| :progress-duration | int | 0 | Tween duration (ms) when progress changes |
| before-id | ?string | null | Insert the route layers below this existing layer id |
| :fit-bounds | bool | false | Fit the map to the route once it is drawn |