marker
StableA marker at a coordinate, with an optional popup and dragging that reports the new position.
Set assigns
Move the marker
transition_max_distance
Drawn as
Add and remove markers
- @lng_lat
- -9.1393, 38.7223
- @color
- #7c5cd6
- @scale
- 1.0
- @draggable
- true
- @popup_text
- Drag me to a new place.
- @content
- pin
- @transition
- 1000
- @transition_max_distance
- nil
- @pins
- 0
<div id="marker-example">
<.example_frame error={@map_error} error_id="example-marker-map-error">
<.map
id="example-marker-map"
center={@home}
zoom={13}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- Markers sit next to <.map>, not inside it, and name it with map_id.
A click on the marker opens its popup above the pin; an empty
popup_text gives it none. A new lng_lat moves the marker over
transition ms, in a straight line, or makes it jump there when the
move is longer than transition_max_distance metres. --%>
<.marker
:if={@content == "pin"}
id="example-marker"
map_id="example-marker-map"
lng_lat={@lng_lat}
color={@color}
scale={@scale}
draggable={@draggable}
popup_text={@popup_text}
transition={@transition}
transition_max_distance={@transition_max_distance}
events={["marker:drag_end"]}
/>
<%!-- The same marker with a slot: drawn as its content, a dot with a ring
that pulses, in Tailwind classes only, and holds still for visitors
who ask for less motion. color and scale draw the pin, so the dot
takes them in its own style. The popup's tip would sit on the dot's
centre: popup_offset puts it 14 px above. --%>
<.marker
:if={@content == "dot"}
id="example-marker"
map_id="example-marker-map"
lng_lat={@lng_lat}
draggable={@draggable}
popup_text={@popup_text}
popup_offset={14}
transition={@transition}
transition_max_distance={@transition_max_distance}
events={["marker:drag_end"]}
>
<span
id="example-marker-dot"
class="relative block size-4 cursor-pointer"
style={"color: #{@color}; scale: #{@scale}"}
>
<span
id="example-marker-dot-ring"
class="absolute inset-0 rounded-full bg-current motion-safe:animate-ping motion-reduce:scale-200 motion-reduce:opacity-30"
/>
<span class="absolute inset-0 rounded-full bg-current ring-2 ring-white" />
</span>
</.marker>
<.marker
:for={pin <- @pins}
id={pin.id}
map_id="example-marker-map"
lng_lat={pin.lng_lat}
color="#63637a"
scale={0.8}
/>
<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; the marker follows. --%>
<.example_group id="example-marker-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{name, hex} <- colors()}
id={"example-marker-color-#{name}"}
code
pressed={@color == hex}
phx-click="color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
<.form
for={@options}
id="example-marker-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:grid-cols-[1fr_auto_2fr] sm:items-end"
>
<.example_range field={@options[:scale]} label="scale" min="0.5" max="2" step="0.1" />
<.example_checkbox field={@options[:draggable]} label="draggable" />
<.input
field={@options[:popup_text]}
type="text"
label="popup_text"
maxlength="60"
phx-debounce="300"
wrapper_class="font-mono text-label text-ink-subtle"
/>
</.form>
</.example_group>
<div class="space-y-4">
<%!-- A new lng_lat from the server: the marker moves there over
transition ms. With reduced motion, it jumps. --%>
<.example_group id="example-marker-moves" label={gettext("Move the marker")}>
<.example_button id="example-marker-move-centre" phx-click="move" phx-value-to="centre">
{gettext("To the centre")}
</.example_button>
<.example_button id="example-marker-move-home" phx-click="move" phx-value-to="home">
{gettext("Back to Lisbon")}
</.example_button>
<.form for={@motion} id="example-marker-motion" phx-change="motion" class="w-full pt-1">
<.example_range
field={@motion[:transition]}
label="transition"
min="0"
max="3000"
step="250"
/>
</.form>
</.example_group>
<.example_group id="example-marker-max-distances" label="transition_max_distance" code>
<.example_button
:for={metres <- max_distances()}
id={"example-marker-max-distance-#{shown_metres(metres)}"}
code
pressed={@transition_max_distance == metres}
phx-click="max_distance"
phx-value-metres={shown_metres(metres)}
>
{shown_metres(metres)}
</.example_button>
</.example_group>
<%!-- The pin, or the content of the marker's slot. --%>
<.example_group id="example-marker-contents" label={gettext("Drawn as")}>
<.example_button
:for={content <- contents()}
id={"example-marker-content-#{content}"}
pressed={@content == content}
phx-click="content"
phx-value-content={content}
>
{content_label(content)}
</.example_button>
</.example_group>
<%!-- Markers added and removed by the server, at the map's centre. --%>
<.example_group id="example-marker-list" label={gettext("Add and remove markers")}>
<.example_button
id="example-marker-add"
phx-click="add_pin"
disabled={length(@pins) >= max_pins()}
>
{gettext("Add at the centre")}
</.example_button>
<.example_button id="example-marker-remove" phx-click="remove_pin" disabled={@pins == []}>
{gettext("Remove the last")}
</.example_button>
</.example_group>
</div>
</div>
<%!-- The assigns as they are now: marker:drag_end keeps lng_lat current. --%>
<.example_readout values={[
lng_lat: lng_lat(@lng_lat),
color: @color,
scale: @scale,
draggable: @draggable,
popup_text: shown(@popup_text),
content: @content,
transition: @transition,
transition_max_distance: shown_metres(@transition_max_distance),
pins: length(@pins)
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="marker-example">
<.example_frame error={@map_error} error_id="example-marker-map-error">
<.map
id="example-marker-map"
center={@home}
zoom={13}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- Markers sit next to <.map>, not inside it, and name it with map_id.
A click on the marker opens its popup above the pin; an empty
popup_text gives it none. A new lng_lat moves the marker over
transition ms, in a straight line, or makes it jump there when the
move is longer than transition_max_distance metres. --%>
<.marker
:if={@content == "pin"}
id="example-marker"
map_id="example-marker-map"
lng_lat={@lng_lat}
color={@color}
scale={@scale}
draggable={@draggable}
popup_text={@popup_text}
transition={@transition}
transition_max_distance={@transition_max_distance}
events={["marker:drag_end"]}
/>
<%!-- The same marker with a slot: drawn as its content, a dot with a ring
that pulses, in Tailwind classes only, and holds still for visitors
who ask for less motion. color and scale draw the pin, so the dot
takes them in its own style. The popup's tip would sit on the dot's
centre: popup_offset puts it 14 px above. --%>
<.marker
:if={@content == "dot"}
id="example-marker"
map_id="example-marker-map"
lng_lat={@lng_lat}
draggable={@draggable}
popup_text={@popup_text}
popup_offset={14}
transition={@transition}
transition_max_distance={@transition_max_distance}
events={["marker:drag_end"]}
>
<span
id="example-marker-dot"
class="relative block size-4 cursor-pointer"
style={"color: #{@color}; scale: #{@scale}"}
>
<span
id="example-marker-dot-ring"
class="absolute inset-0 rounded-full bg-current motion-safe:animate-ping motion-reduce:scale-200 motion-reduce:opacity-30"
/>
<span class="absolute inset-0 rounded-full bg-current ring-2 ring-white" />
</span>
</.marker>
<.marker
:for={pin <- @pins}
id={pin.id}
map_id="example-marker-map"
lng_lat={pin.lng_lat}
color="#63637a"
scale={0.8}
/>
<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; the marker follows. --%>
<.example_group id="example-marker-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{name, hex} <- colors()}
id={"example-marker-color-#{name}"}
code
pressed={@color == hex}
phx-click="color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
<.form
for={@options}
id="example-marker-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:grid-cols-[1fr_auto_2fr] sm:items-end"
>
<.example_range field={@options[:scale]} label="scale" min="0.5" max="2" step="0.1" />
<.example_checkbox field={@options[:draggable]} label="draggable" />
<.input
field={@options[:popup_text]}
type="text"
label="popup_text"
maxlength="60"
phx-debounce="300"
wrapper_class="font-mono text-label text-ink-subtle"
/>
</.form>
</.example_group>
<div class="space-y-4">
<%!-- A new lng_lat from the server: the marker moves there over
transition ms. With reduced motion, it jumps. --%>
<.example_group id="example-marker-moves" label={gettext("Move the marker")}>
<.example_button id="example-marker-move-centre" phx-click="move" phx-value-to="centre">
{gettext("To the centre")}
</.example_button>
<.example_button id="example-marker-move-home" phx-click="move" phx-value-to="home">
{gettext("Back to Lisbon")}
</.example_button>
<.form for={@motion} id="example-marker-motion" phx-change="motion" class="w-full pt-1">
<.example_range
field={@motion[:transition]}
label="transition"
min="0"
max="3000"
step="250"
/>
</.form>
</.example_group>
<.example_group id="example-marker-max-distances" label="transition_max_distance" code>
<.example_button
:for={metres <- max_distances()}
id={"example-marker-max-distance-#{shown_metres(metres)}"}
code
pressed={@transition_max_distance == metres}
phx-click="max_distance"
phx-value-metres={shown_metres(metres)}
>
{shown_metres(metres)}
</.example_button>
</.example_group>
<%!-- The pin, or the content of the marker's slot. --%>
<.example_group id="example-marker-contents" label={gettext("Drawn as")}>
<.example_button
:for={content <- contents()}
id={"example-marker-content-#{content}"}
pressed={@content == content}
phx-click="content"
phx-value-content={content}
>
{content_label(content)}
</.example_button>
</.example_group>
<%!-- Markers added and removed by the server, at the map's centre. --%>
<.example_group id="example-marker-list" label={gettext("Add and remove markers")}>
<.example_button
id="example-marker-add"
phx-click="add_pin"
disabled={length(@pins) >= max_pins()}
>
{gettext("Add at the centre")}
</.example_button>
<.example_button id="example-marker-remove" phx-click="remove_pin" disabled={@pins == []}>
{gettext("Remove the last")}
</.example_button>
</.example_group>
</div>
</div>
<%!-- The assigns as they are now: marker:drag_end keeps lng_lat current. --%>
<.example_readout values={[
lng_lat: lng_lat(@lng_lat),
color: @color,
scale: @scale,
draggable: @draggable,
popup_text: shown(@popup_text),
content: @content,
transition: @transition,
transition_max_distance: shown_metres(@transition_max_distance),
pins: length(@pins)
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.MarkerExample do
@moduledoc """
The example of `<.marker>`: a marker whose colour, scale, dragging and
popup follow assigns (a blank popup text removes the popup, a new one adds
it back), whose dropped position comes back with `marker:drag_end`, and
markers added and removed from the server. The marker is MapLibre's
default pin or, with a slot, its own content: a dot with a pulsing ring,
in CSS only, whose popup `popup_offset` moves off it. A new `lng_lat`
moves it over `transition` milliseconds, unless the move is longer than
`transition_max_distance` metres, which makes it a jump.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/liberty"
@lisbon [-9.1393, 38.7223]
@colors [
{"purple", "#7c5cd6"},
{"teal", "#0f8b8d"},
{"orange", "#d9480f"},
{"red", "#c92a2a"}
]
# What the marker is drawn as: the default pin, or the dot of its slot.
@contents ~w(pin dot)
# How long a move takes, in milliseconds, as the slider offers it, and the
# longest move, in metres, nil for none.
@transitions 0..3000
@max_distances [nil, 1000]
# The most markers the buttons add: each one is kept in the LiveView.
@max_pins 50
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(home: @lisbon, view_center: @lisbon, lng_lat: @lisbon, pins: [], map_error: nil)
|> assign(:content, "pin")
|> assign(transition_max_distance: nil)
|> assign_motion(1000)
|> assign(color: "#7c5cd6", scale: 1.0, draggable: true)
|> assign(popup_text: gettext("Drag me to a new place."))
|> assign_options()
{:ok, socket}
end
# Driven from the server: these only change assigns, and <.marker> follows.
@impl true
def handle_event("color", %{"color" => color}, socket) do
if Enum.any?(@colors, fn {_name, hex} -> hex == color end) do
{:noreply, assign(socket, :color, color)}
else
{:noreply, socket}
end
end
def handle_event("content", %{"content" => content}, socket) when content in @contents do
{:noreply, assign(socket, :content, content)}
end
def handle_event("content", _params, socket), do: {:noreply, socket}
# A new lng_lat moves the marker over `transition` ms instead of jumping
# there: to the map's centre, or back to Lisbon.
def handle_event("move", %{"to" => "centre"}, socket) do
{:noreply, assign(socket, :lng_lat, socket.assigns.view_center)}
end
def handle_event("move", %{"to" => "home"}, socket) do
{:noreply, assign(socket, :lng_lat, socket.assigns.home)}
end
def handle_event("motion", %{"transition" => transition}, socket) do
case Integer.parse(transition) do
{ms, ""} when ms in @transitions -> {:noreply, assign_motion(socket, ms)}
_invalid -> {:noreply, socket}
end
end
# A move longer than transition_max_distance metres is a jump; nil for no
# longest move.
def handle_event("max_distance", %{"metres" => "nil"}, socket) do
{:noreply, assign(socket, :transition_max_distance, nil)}
end
def handle_event("max_distance", %{"metres" => metres}, socket) do
case Integer.parse(metres) do
{metres, ""} when metres in @max_distances ->
{:noreply, assign(socket, :transition_max_distance, metres)}
_invalid ->
{:noreply, socket}
end
end
def handle_event("options", params, socket) do
%{"scale" => scale, "draggable" => draggable, "popup_text" => text} = params
# A blank popup_text gives the marker no popup: it closes and goes.
{:noreply,
socket
|> assign(scale: number(scale), draggable: draggable == "true")
|> assign(:popup_text, String.trim(text))
|> assign_options()}
end
# Markers added and removed from the server: each one is an element in the
# template, so adding or removing one is a change to a list.
def handle_event("add_pin", _params, %{assigns: %{pins: pins}} = socket)
when length(pins) < @max_pins do
id = "example-marker-pin-#{length(pins) + 1}"
pin = %{id: id, lng_lat: socket.assigns.view_center}
{:noreply, update(socket, :pins, &(&1 ++ [pin]))}
end
def handle_event("add_pin", _params, socket), do: {:noreply, socket}
def handle_event("remove_pin", _params, socket) do
{:noreply, update(socket, :pins, &Enum.drop(&1, -1))}
end
# Events from the map and the marker. While the marker is dragged, MaplibreX
# sends marker:dragging at most every 50 ms; marker:drag_end always carries
# the final position, so it is the one to keep.
def handle_event(
"marker:drag_end",
%{"markerId" => "example-marker", "lngLat" => lng_lat},
socket
) do
{:noreply, assign(socket, :lng_lat, lng_lat)}
end
def handle_event("map:moved", %{"center" => center}, socket) do
{:noreply, assign(socket, :view_center, center)}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_options(socket) do
%{scale: scale, draggable: draggable, popup_text: text} = socket.assigns
form = to_form(%{"scale" => scale, "draggable" => draggable, "popup_text" => text})
assign(socket, :options, form)
end
defp assign_motion(socket, transition) do
assign(socket, transition: transition, motion: to_form(%{"transition" => transition}))
end
defp colors, do: @colors
defp max_distances, do: @max_distances
defp max_pins, do: @max_pins
defp shown_metres(nil), do: "nil"
defp shown_metres(metres), do: Integer.to_string(metres)
defp contents, do: @contents
defp content_label("pin"), do: gettext("Default pin")
defp content_label("dot"), do: gettext("Pulsing dot")
# Slider values arrive as strings, "1.6"; the marker's scale is a float.
defp number(value) do
case Float.parse(value) do
{float, _rest} -> float
:error -> 1.0
end
end
defp lng_lat([lng, lat]), do: "#{Float.round(lng / 1, 4)}, #{Float.round(lat / 1, 4)}"
defp shown(""), do: "—"
defp shown(text), do: text
end