MapLibre events come back as ordinary Hologram actions.
Handler syntax
Event props accept the same shapes Hologram's own longhand event bindings do:
# Bare action name, dispatched to the page.
on_click={:parcel_clicked}
# Longhand, when the handler lives on a component rather than the page.
on_click={%{action: :parcel_clicked, target: "sidebar"}}
# With static params merged into the payload.
on_click={%{action: :parcel_clicked, target: "sidebar", params: %{layer: "parcels"}}}A keyword list works wherever a map does. An unset handler attaches no MapLibre listener at all, so an event prop can be driven from state:
on_click={if @selectable do :parcel_clicked end}Throttling and debouncing
Some events come many times a second, such as on_mouse_move. A handler map
can limit how often its action is dispatched, in milliseconds:
# At most one action every 100 ms, and the last event always.
on_mouse_move={%{action: :pointer_moved, throttle: 100}}
# One action, once the events have stopped for 250 ms.
on_mouse_move={%{action: :pointer_rested, debounce: 250}}throttle: ms dispatches the first event at once and opens an interval of
ms. Events that come during it are held, each replacing the one before, and
the one held when the interval ends is dispatched then, opening the next
interval. At most one action is dispatched per interval, and the last event of
a burst always is, at most ms after it came, so the application always
receives where the pointer, the camera or a dragged marker ended up.
debounce: ms dispatches nothing while events keep coming, and the last one
once none has come for ms.
Both work on every event prop, with the same meaning. The payload is the one
the dispatched event carried when it came. 0 dispatches every event, and a
handler carries one of the two, not both. A held event is dropped when its
handler is taken away or replaced, and when the map goes. An event the runtime
leaves out, such as a camera move it made itself (see below), is neither
dispatched nor counted.
Targets
target is a component cid, "page", or "layout". It defaults to "page".
That default differs from Hologram's own, which is the closest enclosing
stateful component. HoloMap's children are deliberately stateless and have
no cid, so there is nothing to fall back to, and silently targeting the
map container would send the action to HoloMap.Map rather than to your
handler.
If your handler lives on a stateful component, name it:
<HoloMap.Layer.Fill id="parcels" source="parcels"
on_click={%{action: :picked, target: "sidebar"}} />Params
Whatever you supply in params is merged under the runtime payload, so the
data MapLibre reports always wins over a static value with the same key. Keys
arrive in your handler as atoms, as they do for any Hologram action.
Payloads
Map events
Each prop listens for the MapLibre event of the same name, on_dbl_click for
dblclick and on_source_data_loading for sourcedataloading:
| Group | Props |
|---|---|
| Lifecycle | on_load, on_idle, on_error, on_resize, on_webgl_context_lost, on_webgl_context_restored |
| Loading | on_data, on_data_loading, on_data_abort, on_source_data, on_source_data_loading, on_source_data_abort, on_style_data, on_style_image_missing |
| Pointer | on_click, on_dbl_click, on_context_menu, on_mouse_move, on_mouse_down, on_mouse_up, on_mouse_over, on_mouse_out, on_wheel, on_touch_start, on_touch_move, on_touch_end, on_touch_cancel, on_cooperative_gesture_prevented |
| Camera | on_move_start, on_move, on_move_end, on_zoom_start, on_zoom, on_zoom_end, on_rotate_start, on_rotate, on_rotate_end, on_pitch_start, on_pitch, on_pitch_end, on_roll_start, on_roll, on_roll_end, on_drag_start, on_drag, on_drag_end, on_box_zoom_start, on_box_zoom_end, on_box_zoom_cancel |
| Rendering | on_render, on_projection_transition, on_terrain |
on_move, on_zoom, on_rotate, on_pitch, on_roll and on_drag fire on
every frame of a move, and on_render on every frame MapLibre draws. Their
handlers are throttled to 100 ms unless they name a throttle or a debounce
of their own (see Throttling and debouncing), so
a move dispatches about ten actions a second and always ends with where the
camera stopped. throttle: 0 dispatches every frame.
Every map event carries the camera:
| Key | Type | Notes |
|---|---|---|
center | [lng, lat] | Camera centre after the event |
zoom | number | |
bearing | number | |
pitch | number | |
bounds | [[w, s], [e, n]] | The visible extent |
Some add what they are about:
| Key | Type | Events | Notes |
|---|---|---|---|
lng_lat | [lng, lat] | Mouse and touch | Where the pointer is, or the centre of the touches |
point | [x, y] | Mouse and touch | The same, in pixels |
error | map | on_error | What failed, see below |
projection | string or list | on_projection_transition | The projection now in place, as the projection prop takes it |
data_type | string | Loading | "source" or "style" |
source_id | string | A source's loading | |
is_source_loaded | boolean | A source's loading | Whether the source has no request left outstanding |
source_data_type | string or nil | A source's loading | "metadata", "content", "visibility" or "idle", or nil for a tile |
id | string | on_style_image_missing | The image a layer asked for that neither the sprite nor a HoloMap.Image holds |
gesture_type | string | on_cooperative_gesture_prevented | "wheel_zoom" or "touch_pan" |
terrain | map or nil | on_terrain | %{source: id, exaggeration: number}, or nil with no terrain |
Nothing else MapLibre hangs on an event crosses into Elixir: not a source's specification, which for GeoJSON holds its data, nor a tile, nor the browser's own event.
A loading state per source takes two handlers. MapLibre fires
sourcedataloading as a source starts a request and sourcedata as one
completes, with is_source_loaded true once none is left:
<HoloMap.Map cid="explorer" style={@style}
on_source_data_loading={:source_loading}
on_source_data={:source_progress}>
def action(:source_loading, %{source_id: id}, component) do
put_state(component, :loading, Map.put(component.state.loading, id, true))
end
def action(:source_progress, %{source_id: id, is_source_loaded: true}, component) do
put_state(component, :loading, Map.delete(component.state.loading, id))
end
def action(:source_progress, _params, component), do: componentThrottling these with throttle: would keep only the latest event of all
sources in each interval, and could drop the one that says a source finished,
so leave them unthrottled, or debounce a summary of your own.
on_webgl_context_lost and on_webgl_context_restored report the browser
taking the map's WebGL context away, and giving it back, when MapLibre builds
the style it had again. The components' sources, layers, images, sprites,
terrain, sky and light are theirs again by then, and changes made in between apply once the
style has loaded; HoloMap.Map's section Losing the WebGL context has the
details. on_style_image_missing names an icon no image provides,
which MapLibre draws nothing for. on_terrain reports every terrain MapLibre
puts in place or takes away: a style's own as it loads, HoloMap.Terrain's,
and a click on HoloMap.Control.Terrain.
def action(:map_moved, params, component) do
put_state(component, :bounds, params.bounds)
enderror always has a message. A failed request (a style, a tile, a sprite)
adds its HTTP status and url, and an error a source reported adds the
source's source_id:
def action(:map_failed, %{error: %{source_id: "dem"} = error}, component) do
put_state(component, :notice, "Elevation tiles are unavailable: #{error.message}")
end
def action(:map_failed, params, component) do
put_state(component, :notice, params.error.message)
endon_projection_transition fires for each projection MapLibre puts in place:
the first one, once the first style has loaded, a change to the map's
projection prop, a click on HoloMap.Control.Globe, and a style change that
puts one in place. Its projection is a name such as "globe", or the
expression the projection came from, so writing it back into the state the
prop reads keeps that state in step with the control and changes nothing on
the map:
def action(:projection_changed, params, component) do
put_state(component, :projection, params.projection)
endOne on_error carries no camera. When MapLibre cannot build the map at all,
because the browser cannot create a WebGL2 context or because it refuses the
map's camera limits, the payload holds error alone, with MapLibre's message,
and arrives once. See HoloMap.Map for how to tell it apart.
Layer events
on_click, on_dbl_click, on_context_menu, on_mouse_enter,
on_mouse_leave, on_mouse_over, on_mouse_out, on_mouse_move,
on_mouse_down, on_mouse_up, on_touch_start, on_touch_end,
on_touch_cancel. In MapLibre GL JS 6, on_mouse_over fires as
on_mouse_enter does, and on_mouse_out as on_mouse_leave.
| Key | Type | Notes |
|---|---|---|
layer | string | The layer's id |
lng_lat | [lng, lat] | |
point | [x, y] | In pixels |
features | list | The features under the pointer |
Each feature carries id, source, source_layer, properties and
geometry. MapLibre's own feature objects hold back-references to their source
and tile which cannot cross into Elixir, so only the actionable parts are
carried over.
Every key is an atom, all the way down
Hologram converts JavaScript object keys to atoms when an action's params cross the boundary, and it does so recursively. A feature therefore arrives like this:
%{
id: 0,
source: "cities",
source_layer: nil,
properties: %{name: "Santo Domingo", population: 2_908_000},
geometry: %{type: "Point", coordinates: [-69.93, 18.48]}
}Your own GeoJSON property names are part of that: a property written as
"parcel_id" in the data is matched as %{parcel_id: id}. Matching with
string keys silently fails. The clause simply does not match, and a case
with a catch-all falls through without an error.
def action(:parcel_clicked, params, component) do
case params.features do
[%{properties: %{parcel_id: id}} | _rest] -> put_state(component, :selected, id)
[] -> put_state(component, :selected, nil)
end
endfeatures is a list because layers overlap, and it can be empty: MapLibre
reports on_mouse_leave without features. Match on it rather than assuming a
head. Map events carry no features at all.
A layer with expand_cluster_on_click zooms in on a clicked cluster by
itself, without an action. Its on_click, if it has one, is dispatched
first, with the click as it was; "Expanding clusters" in HoloMap.Layer has
the details.
Popup events
on_open, on_close. Neither carries a payload.
on_close fires for every close MapLibre performs without asking: the close
button, a click on the map (close_on_click is on by default) and a camera
move. Handle it by clearing whatever state opened the popup. Otherwise the
component still believes the popup is open, and clicking the same feature
again re-renders nothing.
Neither event fires when your application closes the popup by no longer
rendering it, since that close already comes from your state, nor when a
change to anchor, close_button, close_on_click, close_on_move or
focus_after_open replaces an open popup with one built with the new options.
on_open fires when the popup first renders, and when a changed declaration
reopens a popup MapLibre had closed.
def action(:selection_cleared, _params, component) do
put_state(component, :selected, nil)
endMarker events
on_click, on_drag_start, on_drag, on_drag_end.
| Key | Type | Notes |
|---|---|---|
lng_lat | [lng, lat] | For drag events, where the marker has been moved to |
def action(:pin_moved, params, component) do
[lng, lat] = params.lng_lat
put_state(component, :coords, {lng, lat})
endon_drag fires on every pointer move, and writing the position back into state
from it is safe: the value going back is the one MapLibre just reported, so the
setLngLat the reconciler performs is a no-op and the marker stays locked to
the cursor. Prefer on_drag_end when only the final position matters, since there is
no reason to re-render on every frame for a value nothing reads until the drag
ends.
Control events
HoloMap.Control.Geolocate: on_geolocate, on_error,
on_out_of_max_bounds, on_track_start, on_track_end,
on_user_location_focus, on_user_location_lost_focus.
on_geolocate and on_out_of_max_bounds receive the browser's position:
| Key | Type | Notes |
|---|---|---|
coords | map | longitude, latitude and accuracy, in degrees and metres, and altitude, altitude_accuracy, heading and speed, each nil when the device does not give it |
timestamp | number | When the position was taken, in milliseconds since the Unix epoch |
on_error receives the code and message of the browser's error: 1 for
permission denied, 2 for a position unavailable, 3 for a timeout. The
tracking events carry no payload.
<HoloMap.Control.Geolocate track_user_location
on_geolocate={:user_located}
on_error={:location_failed} />
def action(:user_located, %{coords: %{longitude: lng, latitude: lat}}, component) do
put_state(component, :user, {lng, lat})
end
def action(:location_failed, %{code: 1}, component) do
put_state(component, :notice, "Location access was denied.")
endHoloMap.API.trigger_geolocate/1 asks for the position from an action, as a
click on the button does, and the answer arrives through the same events.
HoloMap.Control.Fullscreen: on_enter and on_exit, with no payload.
A change to a control's handlers alone keeps the control and moves its listeners. A change to any other prop rebuilds the control, and its listeners move to the new one.
Hover styling
The usual pattern needs feature ids, which GeoJSON features often lack. Set
generate_id on the source, or point promote_id at a property that is already
unique:
<HoloMap.Source.GeoJSON id="parcels" data={@parcels} generate_id />
<HoloMap.Layer.Fill
id="parcels-fill"
source="parcels"
paint={%{fill_opacity: ["case", ["boolean", ["feature-state", "hover"], false], 0.8, 0.35]}}
on_mouse_move={:parcel_hover}
on_mouse_leave={:parcel_unhover}
/>def action(:parcel_hover, params, component) do
case params.features do
[%{id: id} | _rest] ->
clear_hover(component)
HoloMap.API.set_feature_state("explorer", %{source: "parcels", id: id}, %{hover: true})
put_state(component, :hovered, id)
[] ->
component
end
endWhat is not reported back
A camera move the reconciler itself made, because you changed center or
zoom from state, does not fire on_move_end, nor any other camera event:
on_move_start, on_move, on_zoom and the others. Only a move the user or
the imperative API caused does.
The same holds for a move MapLibre makes to keep the camera within its limits.
When a change to max_bounds, min_zoom, max_zoom, min_pitch or
max_pitch leaves the camera outside the new limits, MapLibre zooms, pitches
or pans it back, and none of on_move_start, on_move_end, on_zoom_end and
on_pitch_end fires for that move. The next move the user makes reports where
the camera ended up. A change to pixel_ratio resizes the canvas, which
MapLibre announces with the events of a move, and those are not reported
either.
Without that, writing the camera from state would echo straight back into an action: a feedback loop at best, and at worst an action arriving on a page that has already changed. If you need to know when your own camera assignment settled, you already do: it is the state change that caused it.
A change to the projection prop is the exception: on_projection_transition
reports it. Its payload carries the projection the prop asked for, so writing
it back into state is no change, and nothing echoes. on_terrain reports
HoloMap.Terrain's terrain the same way. The loading events report the sources
and the style the components declare as they load, since loading is not
something the application did.
Ordering and timing
Hologram.dispatchAction is asynchronous. An event dispatched at the moment a
navigation begins can execute after the new page has mounted, which is why
HoloMap refuses to dispatch from a map whose container has gone stale. Your
handlers do not need to defend against this, but it is worth knowing why an
action can appear to arrive late.
A click is reported once the browser has finished handling it: the on_click
of the map, a layer or a marker is dispatched after the on_close of any popup
that the same click closed. A popup whose on_close clears the selection
therefore never undoes the selection the click makes.