Most of a map belongs in props. Some of it does not.

When to reach for it

"Fly to the result the user just picked" is an event, not a value. Re-rendering the same centre twice should not re-animate, and animating is the whole point. That is the category HoloMap.API exists for.

SituationUse
The camera reflects application stateHoloMap.Map's center / zoom props
The camera should animate in response to an eventfly_to/2, ease_to/2
A layer's paint reflects stateHoloMap.Layer.*'s paint prop
A one-off highlight your state does not trackset_paint_property/4
Hover and selection stylingset_feature_state/3
A question for the map, such as which features are in a boxquery_rendered_features/2, request/4
Zoom in on a clicked clustera layer's expand_cluster_on_click, or expand_cluster/4
The points in a clusterget_cluster_leaves/4

An imperative change lasts until the reconciler sets the same thing from the props. A paint or layout property is set again when that property's declared value changes, a filter when the declared filter changes, and the camera when a declared center, zoom, bearing or pitch changes, in which case only the values that changed are set. A change to any other prop leaves the imperative value in place, so an imperative fill-color survives a new fill-opacity or filter. A layer that is added again, because its type, source or source_layer changed or its source was rebuilt, comes back as declared, and so does every layer when the style changes. The reconciler treats rendered props as the source of truth.

Calling it

Always from an action, always with the map's cid first:

def action(:show_parcel, params, component) do
  HoloMap.API.fly_to("explorer", center: {params.lng, params.lat}, zoom: 17)
  put_state(component, :selected, params.id)
end

Actions only

These calls compile to no-ops on the server. They work in action/3 and in functions an action reaches; they do nothing in init/3 or command/3.

To move the camera at mount time, chain an action from init:

def init(_params, component, _server) do
  component
  |> put_state(:style, @style_url)
  |> put_action(:frame_data)
end

Calls made before the map has booted, or before its style finishes loading, are queued and replayed in order, so an action scheduled from init is not lost, even when it runs before the map's own boot action. Calls for a map that has not booted within five seconds are dropped with a console warning. A call for a map torn down with HoloMap.Runtime.destroy/1 while its container stays on the page is dropped at once, with a warning, until the map boots again. So is a call for a map MapLibre could not build, such as one on a browser without WebGL2, while its container stays on the page: the warning names MapLibre's error, and the calls queued for the map are dropped with it as soon as the map fails.

Camera

HoloMap.API.fly_to("explorer", center: {-70.66, 19.45}, zoom: 14, duration: 1_200)
HoloMap.API.ease_to("explorer", bearing: 45, pitch: 60)
HoloMap.API.jump_to("explorer", center: {-70.66, 19.45})
HoloMap.API.fit_bounds("explorer", [-71.0, 19.0, -70.0, 20.0], padding: 48)
HoloMap.API.resize("explorer")

fly_to/2 arcs out and back in, which reads well over long distances. ease_to/2 travels in a straight line, which reads better over short ones. jump_to/2 does not animate, which is what a center prop change does.

:easing shapes an animated move's progress over its duration. MapLibre takes it as a JavaScript function, which Elixir cannot send, so it takes the name of one of CSS's timing curves, :linear, :ease_in, :ease_out or :ease_in_out, or a curve of your own as {:cubic_bezier, x1, y1, x2, y2}, and the browser runtime builds the function. Anything else raises ArgumentError. HoloMap.API.fly_to/2 documents each curve.

HoloMap.API.ease_to("explorer", bearing: 90, duration: 900, easing: :ease_in_out)
HoloMap.API.fly_to("explorer", center: {-70.66, 19.45}, easing: {:cubic_bezier, 0.3, 0, 0.2, 1})

fit_bounds/3 accepts any shape HoloMap.Spec.bounds/1 understands: a pair of corners, or a flat [west, south, east, north] list.

resize/1 is almost never needed. MapLibre observes its own container, so a map whose container changes size follows along by itself and this call does nothing. The one case where it is required is a map built with track_resize={false}, which switches that observer off; the demo's camera page has one set up that way so the difference is visible.

Layers

HoloMap.API.set_filter("explorer", "parcels-fill", ["==", ["get", "zone"], "R1"])
HoloMap.API.set_paint_property("explorer", "parcels-fill", :fill_color, "#ef4444")
HoloMap.API.set_layout_property("explorer", "parcels-fill", :visibility, "none")

Property names may be atoms in Elixir style or MapLibre specification names; both reach MapLibre as the latter.

These duplicate what the paint, layout and filter props already do, and in almost every case the props are the better choice: a style change, or a structural source change that adds a layer again, gives the layer its props back, and loses whatever was set here. Reach here for something genuinely transient.

Feature state

Feature state is the mechanism behind hover and selection styling, and it has no declarative equivalent: it belongs to a feature, not to a component.

HoloMap.API.set_feature_state("explorer", %{source: "parcels", id: 12}, %{hover: true})
HoloMap.API.remove_feature_state("explorer", %{source: "parcels", id: 12}, "hover")
HoloMap.API.remove_feature_state("explorer", %{source: "parcels", id: 12})

Add :source_layer for a vector source. Every feature needs an id, which GeoJSON features often lack. Set generate_id on the source, or point promote_id at a property that is already unique. There is a worked example in Events.

Feature state lasts as long as the source does. A style change MapLibre diffs keeps it on the sources the components declared. One it builds from scratch clears it, as a structural change to the source does, and so does a WebGL context loss, after which MapLibre builds the style again: set it again from on_webgl_context_restored if it matters.

Clusters

A clustered HoloMap.Source.GeoJSON gathers nearby points into clusters, each a feature with cluster: true, a cluster_id and a point_count in its properties. The usual way to open one up is a click that zooms in to where it breaks apart, which a layer does by itself with expand_cluster_on_click. expand_cluster/4 makes the same move from an action, with the options of ease_to/2:

def action(:cluster_clicked, params, component) do
  case params.features do
    [%{properties: %{cluster_id: id}, geometry: %{coordinates: point}} | _rest] ->
      HoloMap.API.expand_cluster("explorer", "stops", id, center: point, duration: 300)

    _not_a_cluster ->
      :ok
  end

  component
end

Without :center, the camera zooms in about its current centre. The cluster's point, the feature's geometry.coordinates, brings it to the middle.

The points inside a cluster, for a list or a popup, come back through the reply channel described below, as features:

HoloMap.API.get_cluster_leaves("explorer", "stops", id, limit: 20, reply: :leaves_loaded)

def action(:leaves_loaded, %{result: leaves, truncated: more}, component) do
  put_state(component, leaves: leaves, more: more)
end

:limit is how many points the answer holds, 100 unless given, and :offset how many it skips. While truncated is true, the next page starts at offset + limit. get_cluster_children/4 answers with what the cluster breaks into at the next zoom instead: smaller clusters and single points.

Asking the map

The calls above tell the map to do something, and return :ok at once. A question, such as which features are in a box or where a coordinate is on screen, has an answer, and the answer lives in the browser. So a question names the action that receives it, with :reply, and the answer arrives there as that action's params:

def action(:select_in_box, params, component) do
  HoloMap.API.query_rendered_features("explorer",
    box: {{params.x1, params.y1}, {params.x2, params.y2}},
    layers: ["parcels-fill"],
    reply: :parcels_found
  )

  component
end

def action(:parcels_found, %{error: error}, component) do
  put_state(component, :notice, error.message)
end

def action(:parcels_found, %{result: features, truncated: truncated}, component) do
  component
  |> put_state(:selection, features)
  |> put_state(:more_than_shown, truncated)
end

:reply takes the shapes an event prop takes: an action name, which goes to the page, or %{action: ..., target: ..., params: ...} for an action on a component. Its params are merged under the answer, so an application that sends several questions to one action can tell the answers apart:

HoloMap.API.request("explorer", "getZoom", [], reply: %{action: :read, params: %{ref: "zoom"}})
HoloMap.API.request("explorer", "getBounds", [], reply: %{action: :read, params: %{ref: "bounds"}})

def action(:read, %{ref: "zoom", result: zoom}, component), do: put_state(component, :zoom, zoom)
def action(:read, %{ref: "bounds", result: bounds}, component), do: put_state(component, :bounds, bounds)

The params travel as JSON, as an event prop's do, so an atom among their values comes back as a string.

The answer's params hold one of two keys:

  • :result, the answer as plain data. Coordinates are [lng, lat] lists, pixel positions [x, y], and bounds [[west, south], [east, north]], as in event payloads, and features are maps with id, source, source_layer, properties and geometry, as layer events carry them.
  • :error, %{message: message}, when MapLibre throws or reports an error while it answers, such as for a layer the style does not hold (which the map's on_error hears as well), or when there is no map to answer: it did not boot within five seconds, MapLibre could not build it, or HoloMap.Runtime.destroy/1 tore it down before it answered.
QuestionFunction:result
Features MapLibre rendered, in the viewport, at a point or in a boxquery_rendered_features/2features
Features a source holds, rendered or notquery_source_features/3features
Where a coordinate is on screenproject/3[x, y]
The coordinate at a pixel positionunproject/3[lng, lat]
The terrain's elevation at a coordinatequery_terrain_elevation/3metres, or nil without terrain
The camera that fits a boxcamera_for_bounds/3%{center: [lng, lat], zoom: z, bearing: b}, or nil
A feature's stateget_feature_state/3a map
The points in a cluster, a page at a timeget_cluster_leaves/4features
What a cluster breaks into at the next zoomget_cluster_children/4features
Anything else, by method namerequest/4see HoloMap.API.request/4

request/4 reaches any method of the map, and with :source any method of one of its sources, including one that returns a promise, which is answered when the promise settles. Its documentation lists what the getters MapLibre has, such as getCenter, getStyle, listImages and areTilesLoaded, answer.

How big an answer can be

An answer crosses into Elixir as an action's params, which Hologram builds term by term in the browser, so a large answer costs time on every request. A query for features is bounded: its answer carries at most 100 features, or the number given with :limit, and truncated: true says some were left out. geometry: false leaves out each feature's geometry, usually most of a feature's size, when the application needs only ids and properties. Other answers are as big as what they describe: a camera, a list of ids, a feature's state. getStyle is the exception, as it carries the whole style, inline GeoJSON data included.

When the answer arrives

A question waits, as a call does, for a map that has not booted and for a style that is loading, and is answered after the calls made before it. So set_feature_state/3 followed by get_feature_state/3 reads the state just set, and a query made while a new style loads finds the new style's features. A map that fails to build, or that HoloMap.Runtime.destroy/1 tears down, answers with an :error rather than leaving the action waiting. Once a navigation has taken the page away, an answer still on its way is dropped: the page that asked is not there to hear it.

Anything else

HoloMap.Runtime.call/3 reaches any method on the underlying maplibregl.Map:

HoloMap.Runtime.call("explorer", "setRenderWorldCopies", [false])

It queues against style readiness like everything else, but it does not normalise its arguments, and whatever the method returns is discarded. Use HoloMap.API.request/4 for a method whose answer you need.

The arguments cross into the browser as JSON, as every HoloMap.API call's do. An atom arrives as its name, so :top reaches MapLibre as "top" rather than as the browser's window.top, which Hologram's interop would make of it. A tuple arrives as a list and nil as null. A struct, a function or a pid raises ArgumentError: a JavaScript object, such as an image element, belongs in your own JavaScript, through the map window.HoloMap.map(cid) returns.

From JavaScript

A MapLibre plugin (a draw control, a geocoder, a deck.gl overlay) needs the raw map object, and Hologram's interop cannot hand one to a third-party script. Every booted map is published on window:

window.HoloMap.map("explorer")      // the maplibregl.Map, or null
window.HoloMap.cids()               // every live map's cid
window.HoloMap.destroy("explorer")  // release its WebGL context

Prefer HoloMap.API from Elixir: it queues correctly against the style's readiness, which a raw handle does not.