HoloMap.Marker (HoloMap v0.4.0)

Copy Markdown View Source

A pin at a coordinate, rendered as a DOM element above the map.

<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}} color="#ef4444" />

With custom markup, given as slot content:

<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}} on_click={:office_clicked}>
  <div class="rounded-full bg-white px-2 py-1 shadow">Head office</div>
</HoloMap.Marker>

Markers versus symbol layers

A marker is a DOM node. That makes it easy to style with your own CSS and cheap to reason about, and expensive at scale. A few dozen is comfortable, a few thousand is not. Past that, put the points in a HoloMap.Source.GeoJSON and draw them with HoloMap.Layer.Circle or HoloMap.Layer.Symbol, which render on the GPU. HoloMap.Image is what lets that symbol layer use an icon of your own.

Props

PropTypeNotes
idstringRequired. Identity across re-renders
lng_lat{lng, lat}Required. Where the marker sits
colorstringFill colour of the default pin. Ignored when slot content is given
scalenumberSize multiplier for the default pin
draggablebooleanLet the user move the marker
rotationnumberRotation in degrees
offset[x, y]Pixel offset from the anchor point
anchorstring"center", "top", "bottom", "left", "right" and the corners
pitch_alignment / rotation_alignmentstring"map", "viewport" or "auto"
opacitystringCSS opacity
opacity_when_coverednumber or stringCSS opacity while 3D terrain or the globe covers the marker. MapLibre's default is "0.2"
class_namestringExtra classes on the marker element
popup_htmlstringAttach a popup that opens when the marker is clicked, its content inserted as markup
popup_textstringAttach that popup with plain text, never read as markup. Wins over popup_html
popup_optionsmapOptions for that popup, in HoloMap.Popup's vocabulary. String keys reach MapLibre unchanged
popup_openbooleanOpen or close that popup from state. See The popup

The popup

A click on the marker opens its popup and a second click closes it, as in MapLibre. With focus_after_open, which is on unless popup_options turns it off, MapLibre moves keyboard focus into a popup the user opens.

popup_open opens the popup from the application's state, and closes it, as MapLibre's togglePopup does:

<HoloMap.Marker id="depot" lng_lat={@depot} popup_text="Main depot"
                popup_open={@selected == "depot"} />

It applies when the marker is built with it set to true, and when its value changes. An open it asks for takes keyboard focus as focus_after_open says. In between, the marker's popup is the user's: a click on the marker still opens and closes it, and MapLibre still closes it on a map click, and a render that leaves popup_open as it was leaves the popup as the user left it. Set it to false and back to true to open the popup again, or set it to nil to leave the popup to the user's clicks alone. It does nothing for a marker without popup_html or popup_text. MapLibre neither opens the popup of a marker that terrain covers nor lets it stay open there, and tells a covered marker by its opacity: a marker whose opacity equals its opacity_when_covered, "0.2" unless set, never opens its popup, by a click or by popup_open.

MapLibre takes the slot content, color, scale, class_name, anchor and click_tolerance only while it builds a marker, and most popup options only while it builds a popup, so a change to one of them, or to popup_options, builds the marker or its popup again. A popup the user had open opens again on the new one, without taking keyboard focus: the user opened nothing. Its next open takes focus as focus_after_open says. A popup the user had closed stays closed, unless popup_open changes in the same render, which opens or closes the new one. A change to popup_html or popup_text alone updates the popup in place, and MapLibre moves focus into an open popup whose content changes, as it does for any popup.

popup_html is inserted as markup

MapLibre inserts popup_html as markup, and Hologram escapes none of the three ways to write it:

  • A plain literal, popup_html="<b>hi</b>", arrives with its character references decoded, so popup_html="&lt;b&gt;" is the markup <b>.
  • A literal with an inserted value, popup_html="Hi {@name}", arrives as the text and the value joined, neither escaped. A @name of <img src=x onerror=alert(1)> is inserted as an img element with an onerror handler.
  • An expression, popup_html={@popup_html}, arrives as it is, so an escaped string such as popup_html={"&lt;b&gt;"} stays escaped and shows the characters.

The application must escape an untrusted value before it interpolates it. Plug.HTML.html_escape/1, which comes with Phoenix, does that. Call it on the server, in init/3 or a command:

name = Plug.HTML.html_escape(place.name)
put_state(component, :popup_html, "Hi #{name}")

A value that only needs showing, such as a place name, is safer in popup_text, which MapLibre's setText puts in a text node: popup_text={@name} shows <img src=x onerror=alert(1)> as those characters. A marker given both popup_text and popup_html shows the text in its popup and logs a console warning, since showing the html instead would silently drop the choice of text. Either updates the popup in place when it changes. See the Limitations guide.

Events

on_click, on_drag_start, on_drag, on_drag_end. Handlers receive lng_lat, which for the drag events is the position the marker moved to:

def action(:pin_moved, params, component) do
  [lng, lat] = params.lng_lat
  put_state(component, coords: {lng, lat})
end

Dragging

Set draggable and the user can move the marker. on_drag_start fires once, on_drag on every pointer move, on_drag_end once at the finish.

Writing the position back into state from on_drag is safe, and is the usual way to keep a coordinate readout live. It looks like it should not be: each event is a state update, a re-render, and a setLngLat back into the marker MapLibre is in the middle of dragging. It works because the value going back is the one MapLibre just reported, so the write is a no-op. The marker stays locked to the cursor, measured at zero pixels of drift.

<HoloMap.Marker id="pin" lng_lat={@pin} draggable
                on_drag={:pin_moved} on_drag_end={:pin_settled} />

Use on_drag_end alone when only the final position matters; there is no reason to re-render on every frame for a value nothing reads until the drag is over.

Slot content is cloned

MapLibre moves a marker's element into its own overlay container. Handing it the node Hologram rendered would take that node out from under the virtual DOM and corrupt the next patch, so HoloMap gives MapLibre a clone instead. The markup renders and restyles normally, but Hologram event bindings inside the slot never fire. Use on_click on the marker itself. See the Limitations guide.

Summary

Functions

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Returns the list of property definitions for the compiled component.

Functions

__is_hologram_component__()

@spec __is_hologram_component__() :: boolean()

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Examples

iex> __is_hologram_component__()
true

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.