Routes

Docs / 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:

The directions-profile accepts driving, walking, or cycling. The OSRM server URL can be configured in config/livewire-mapcn.php.

Animated Route

Animate the route as it draws on the map. The line grows smoothly by distance over :animate-duration milliseconds:

blade
<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:

40%

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:

php / js
// 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.

PropTypeDefaultDescription
atstring|float'progress'start, end, progress or a 0–1 fraction along the route
:align-to-routeboolfalseRotate the marker to the route heading at its position
route-id?stringnullRoute to attach to when not nested inside <x-map-route>
id?stringuuidMarker id used in marker events
colorstring'#1A56DB'Color of the default dot
anchorstring'center'Which part of the marker sits on the route
:offsetarray[0, 0]Pixel offset [x, y]
:rotationfloat0Rotation in degrees (added to the heading when aligned)
rotation-alignmentstring'auto'map, viewport or auto
pitch-alignmentstring'auto'map, viewport or auto
classstring''Additional CSS classes

<x-map-route-group>

Render an array of routes where one can be "active" or "selected":

PropDescription
:routesArray of route objects with color, coordinates, etc.
idUnique identifier
:selected-routeIndex or ID of the initially selected route (Default: 0)
:fit-boundsAuto-fit map bounds to show all routes (Default: false)
:fetch-directionsFetch directions for all routes (Default: false)
:animateAnimate route drawing (Default: false)
classWrapper container classes

<x-map-route-list>

Overlay UI panel displaying route alternatives and details:

PropDescription
route-idID of the x-map-route to link to
map-idDeprecated: the list now finds its route by route-id anywhere on the page
:show-distanceShow total distance (Default: true)
:show-durationShow travel time (Default: true)
:show-fastest-badgeHighlight fastest route (Default: true)
:show-time-diffShow time difference between alternatives (Default: true)
positionOptions: top-left, top-right, etc. (Default: 'top-left')
titlePanel header text (Default: 'Routes')
widthExtra width class for the panel, e.g. w-60 (Default: '', the package CSS sets a sensible width)
container-classExtra classes for the wrapper container
header-classExtra classes for the header section
item-classExtra classes for individual route items

Props

PropTypeDefaultDescription
:coordinatesarrayrequiredArray of [lng, lat] pairs
id?stringnullUnique identifier for the route
colorstring'#1A56DB'Route line color
:widthint|float4Pixel width
:opacityfloat1.0Line opacity (0.0-1.0)
:dash-array?arraynullDash pattern array
line-capstring'round'Line cap style
line-joinstring'round'Line join style
active-colorstring'#0F43C4'Color when active/selected
:active-widthint6Width when active/selected
:active-opacity?floatnullOpacity when active (falls back to :opacity)
:active-dash-array?arraynullDash pattern when active (falls back to :dash-array)
hover-color?stringnullColor on hover
:clickablebooltrueEnable click interactions
:with-stopsboolfalseDraw a dot at each waypoint passed in :coordinates
stop-colorstring'#1A56DB'Color for stop markers
:fetch-directionsboolfalseAuto-fetch geom from OSRM
directions-profilestring'driving'driving, walking, cycling
directions-url?stringnullCustom OSRM server URL
:animateboolfalseAnimate the line growing by distance
:animate-durationint2000Animation duration (ms)
:activeboolfalseSet as initially active
:alternativesboolfalse(OSRM) Fetch alternative routes
:max-alternativesint2Max alternatives to fetch
alternative-colorstring'#94A3B8'Color for alternatives
:alternative-opacityfloat0.5Opacity for alternatives
:alternative-widthint3Width for alternatives
:progress?floatnull0–1. Draws a "travelled" line on top of the route
progress-color?stringnullProgress line color (defaults to the route color)
:progress-widthint|float|nullnullProgress line width (defaults to the route width)
:progress-opacity?floatnullProgress line opacity (defaults to 1)
:progress-dash-array?arraynullProgress line dash pattern (solid by default)
progress-model?stringnullAlpine expression the progress follows on the client (no server request per change), e.g. progress from x-data="{ progress: $wire.entangle('progress') }"
:progress-durationint0Tween duration (ms) when progress changes
before-id?stringnullInsert the route layers below this existing layer id
:fit-boundsboolfalseFit the map to the route once it is drawn