map
StableA MapLibre GL JS map whose center, zoom, pitch, bearing and style follow the LiveView's assigns.
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>
The events this component sends to the LiveView, as the library documents them. Each arrives in handle_event/3. Use the preview; the last payload of each event the example receives appears here. A component sends only the events its events attribute names, so the example receives those its LiveView handles.
-
map:moved
Fired when the map is moved (pan, zoom, rotate, pitch)
Not received yet.
-
map:clicked
Fired when the map is clicked
Not received yet.
-
map:loaded
Fired when the map finishes loading
Not received yet.
-
map:zoom_changed
Fired when zoom level changes
Not received yet.
-
map:error
Fired when MapLibre reports an error, such as a style, a source or a tile that could not be loaded. Besides the message, its params name the source, the HTTP status, the URL and the tile when MapLibre gives them. See "Errors" in the module documentation
Not received yet.
-
map:follow_ended
Fired when following the marker or the feature
follownames ends whilefollowstill names it: the user took the map ("reason" => "user"), or a camera command moved its centre ("reason" => "command"). Its params name the marker ("markerId"), or the layer and the feature ("layerId"and"featureId"). See "Following a marker" and "Following a feature of a layer" in the module documentationNot received yet.