HoloMap.Marker (HoloMap v0.2.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
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_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.