map
StableA MapLibre GL JS map whose center, zoom, pitch, bearing and style follow the LiveView's assigns.
Loading map
Set assigns
Run commands
fly
Once per mount. With reduced motion, the map opens where it lands.
- @place
- madrid
- @markers
- true
- @center
- -3.7038, 40.4168
- @zoom
- 11
- @pitch
- 0
- @bearing
- 0
- @last_click
- —
- @follow
- nil
- @follow_ended
- nil
- @max_zoom
- 22
- @interactive
- true
- @attribution_control
- true
<div id="map-example">
<.example_frame error={@map_error} error_id="example-map-error">
<%!-- fly: the map opens far out and flies to its camera, once, when it
mounts; with reduced motion it opens there at once. projection:
the flight starts from a globe, whatever the basemap declares.
curtain: the map stays covered until its first render is complete,
and the flight starts when the curtain lifts. follow: the id of
a marker the camera keeps at the centre until the user drags the
map; the map then pushes map:follow_ended. events: the map
pushes these events to the LiveView, which has a handle_event/3
clause for each, and no other; without it, it pushes none.
Every other attribute follows its assign after mount. --%>
<.map
:if={@mounted}
id="example-map"
center={@center}
zoom={@zoom}
pitch={@pitch}
bearing={@bearing}
max_zoom={@max_zoom}
interactive={@interactive}
attribution_control={@attribution_control}
style={basemap(@theme)}
fly={%{from: %{zoom: 1.5}}}
projection="globe"
curtain
follow={followed(@follow, @place)}
events={["map:moved", "map:clicked", "map:error", "map:follow_ended"]}
class="h-full w-full"
>
<%!-- Nested in <.map>: rendered right after the map element, so they
follow the assigns and mount and unmount like components placed
next to it. The chosen place's marker is purple. --%>
<.marker
:for={place <- place_list()}
:if={@markers}
id={"example-map-place-#{place.id}"}
map_id="example-map"
lng_lat={place.center}
color={marker_color(place.id, @place)}
/>
</.map>
</.example_frame>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[1fr_auto]">
<%!-- Assigns: each control changes an assign on the server; the map follows. --%>
<.example_group id="example-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{id, name} <- places()}
id={"example-place-#{id}"}
pressed={@place == id}
phx-click="go"
phx-value-place={id}
>
{name}
</.example_button>
<.form
for={@camera}
id="example-camera"
phx-change="camera"
class="grid w-full grid-cols-3 gap-4 pt-2"
>
<.input
field={@camera[:zoom]}
type="range"
label="zoom"
min="1"
max="18"
step="0.25"
wrapper_class="font-mono text-label text-ink-subtle"
class="mt-1 block w-full accent-accent"
/>
<.input
field={@camera[:pitch]}
type="range"
label="pitch"
min="0"
max="60"
wrapper_class="font-mono text-label text-ink-subtle"
class="mt-1 block w-full accent-accent"
/>
<.input
field={@camera[:bearing]}
type="range"
label="bearing"
min="-180"
max="180"
wrapper_class="font-mono text-label text-ink-subtle"
class="mt-1 block w-full accent-accent"
/>
</.form>
<.form
for={@limits}
id="example-limits"
phx-change="limits"
class="grid w-full gap-4 pt-1 sm:grid-cols-[1fr_auto_auto] sm:items-end"
>
<.example_range field={@limits[:max_zoom]} label="max_zoom" min="4" max="22" step="1" />
<.example_checkbox field={@limits[:interactive]} label="interactive" />
<.example_checkbox field={@limits[:attribution_control]} label="attribution_control" />
</.form>
<div class="flex flex-wrap gap-x-6 pt-1">
<.form for={@markers_form} id="example-markers" phx-change="markers">
<.example_checkbox field={@markers_form[:markers]} label=":if markers" />
</.form>
<%!-- Follows the chosen place's marker; another place moves the
camera to its marker. Dragging the map ends it. --%>
<.form for={@follow_form} id="example-follow" phx-change="follow">
<.example_checkbox field={@follow_form[:follow]} label="follow" />
</.form>
</div>
</.example_group>
<div class="space-y-4">
<%!-- Commands: JS structs that run in the browser, with no round trip. --%>
<.example_group id="example-commands" label={gettext("Run commands")}>
<.example_button id="example-zoom-in" code phx-click={MapCmd.zoom_in("example-map")}>
zoom_in
</.example_button>
<.example_button id="example-zoom-out" code phx-click={MapCmd.zoom_out("example-map")}>
zoom_out
</.example_button>
<.example_button
id="example-reset-north"
code
phx-click={MapCmd.reset_north("example-map")}
>
reset_north
</.example_button>
<.example_button
id="example-fly-home"
code
phx-click={MapCmd.fly_to("example-map", @home.center, @home.zoom, duration: 2000)}
>
fly_to
</.example_button>
</.example_group>
<%!-- fly is read when the map mounts: the replay mounts a new map. --%>
<.example_group id="example-fly" label="fly" code>
<.example_button id="example-fly-replay" phx-click="replay">
{gettext("Replay the flight")}
</.example_button>
<.example_note id="example-fly-note" icon="hero-information-circle-micro">
{gettext("Once per mount. With reduced motion, the map opens where it lands.")}
</.example_note>
</.example_group>
</div>
</div>
<%!-- The assigns as they are now: map:moved and map:clicked keep them current. --%>
<.example_readout values={[
place: @place,
markers: @markers,
center: lng_lat(@center),
zoom: @zoom,
pitch: @pitch,
bearing: @bearing,
last_click: lng_lat(@last_click),
follow: followed(@follow, @place) || "nil",
follow_ended: @follow_ended || "nil",
max_zoom: @max_zoom,
interactive: @interactive,
attribution_control: @attribution_control
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the map |
| center | list |
nil
|
Map center coordinates as [longitude, latitude]. Without it, or with nil, the map opens at the configured default_center, [0, 0] unless configured (see MaplibreX). A new value after mount moves the map there at once, at the zoom it has, also on a globe (see "Projection"); nil after mount leaves the map where it is. A centre the map itself has just reported in map:moved, rendered back by the LiveView, is not applied to it again
|
| zoom | any |
nil
|
number |
| style | any |
nil
|
Map style URL or style object. Without it, or with nil, the configured default_style. A new value after mount replaces the style; the layers, sources and terrain of the map's components come back on the new style, and changes to them that arrive while it loads (in the same update, say) are applied once it is in place
|
| min_zoom | any |
nil
|
number |
| max_zoom | any |
nil
|
number |
| min_pitch | any |
nil
|
number, degrees |
| max_pitch | any |
nil
|
number, degrees, up to 180 |
| bearing | any |
0
|
number, degrees |
| pitch | any |
0
|
number, degrees |
| bounds | list |
nil
|
Fit map to bounds [[west, south], [east, north]]. Optional. The map opens on them, whatever center and zoom say. New bounds after mount fit the map to them at once, with no animation and no padding, as when it opens, and win over a center or zoom changed in the same render; no longer giving them leaves the map where it is. For an animated fit, use fit_bounds/3
|
| max_bounds | list |
nil
|
Maximum bounds the map can be panned to. Optional. A new value after mount applies at once; no longer giving it lifts the limit |
| interactive | boolean |
true
|
Whether the user can move the map with the mouse, touch and the keyboard. Defaults to true. After mount, false turns off every one of MapLibre's interaction handlers and true turns them all on again, also on a map created with false. The JS and server-pushed commands move the map either way
|
| attribution_control | boolean |
true
|
Show MapLibre's attribution control, in the bottom-right corner. Defaults to true. After mount, false removes it and true adds it back. An <.attribution_control> (MaplibreX.Components.AttributionControl) on the map takes its place while it is there, so that the map shows one attribution control
|
| fly | any |
nil
|
true, or a map of :from, :duration or :speed, and :curve; read at mount only
|
| projection | any |
nil
|
a projection name, such as "globe" or :globe, or MapLibre's projection specification as a map
|
| curtain | any |
nil
|
true, or a map of :timeout and :label; read at mount only
|
| follow | any |
nil
|
the id of a marker on the map, or {layer_id, feature_id} for a feature of a <.geojson_layer>, which the camera keeps at the centre; nil or false for none
|
| debug | boolean |
false
|
log MaplibreX's lifecycle messages in the browser console, for the whole page |
| class | string |
"maplibrex-map"
|
CSS classes for the map container. Read when the map mounts only: LiveView does not patch the attributes of the map element, which is phx-update="ignore", except its data-* ones. Put classes that change on a wrapper element
|
| testid | string |
nil
|
Test ID for the map element (data-testid), patched like any data-* attribute
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |
| events | any |
[]
|
a list of event names, or :all
|
Slots
- inner_block Optional.
- loader replaces the curtain's built-in loader; ignored without `curtain`