Skip to content

marker

Stable

A 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
lib/maplibrex_demo_web/examples/marker_example.html.heex
<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>