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>
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 marker |
| map_id required | string | — | ID of the map to add the marker to |
| lng_lat required | list | — |
Marker position as [longitude, latitude]
|
| color | string |
"#3FB1CE"
|
Color of the default pin (hex or named color). Defaults to "#3FB1CE". A marker with a slot ignores it
|
| scale | float |
1.0
|
Scale of the default pin. Defaults to 1. A marker with a slot ignores it
|
| rotation | integer |
0
|
Marker rotation in degrees, clockwise. Defaults to 0. With transition, a new rotation turns the short way over that time
|
| draggable | boolean |
false
|
Whether the marker can be dragged. Defaults to false
|
| anchor | string |
nil
|
Marker anchor point. One of: "center", "top", "bottom", "left", "right", etc.
|
| offset | list |
nil
|
Pixel offset as [x, y]
|
| pitch_alignment | string |
"auto"
|
How marker is aligned to pitch. One of: "map", "viewport", "auto"
|
| rotation_alignment | string |
"auto"
|
How marker is aligned to rotation. One of: "map", "viewport", "auto"
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |
| popup_text | string |
nil
|
Text of a popup that a click on the marker opens and closes, above the pin (at the position of a marker with a slot; see Content in the module docs). Shown as plain text: HTML in it is not interpreted |
| popup_html | string |
nil
|
HTML of that popup, used when popup_text is not given. It is inserted as is, so sanitise anything that comes from users. For popup content written in HEEx, render a <.popup> at the marker's position instead
|
| popup_focus_after_open | boolean |
true
|
Whether that popup moves the keyboard focus to its first focusable element (its close button, or a link or button in popup_html) as it opens, MapLibre's focusAfterOpen. Defaults to true; false leaves the focus where it is. A change rebuilds the popup, open if it was
|
| popup_offset | any |
nil
|
the offset of the popup's tip from the marker's position: a number, [x, y], or a map of anchor to [x, y] |
| transition | integer |
nil
|
how long a move to a new lng_lat or rotation takes, in milliseconds; nil or 0 jumps |
| transition_max_distance | any |
nil
|
the longest move, in metres on the ground: a longer one jumps; nil for no limit |
| events | any |
[]
|
a list of event names, or :all
|
Slots
- inner_block Optional.