HoloMap.API (HoloMap v0.2.0)

Copy Markdown View Source

Imperative commands for things that are not state.

Most of a map belongs in component props: which layers exist, how they are painted, what the camera is looking at. Some of it does not. "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.

These functions are for that second category. Call them from an action:

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

The first argument is always the map's cid, the same string given to HoloMap.Map.

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.

Choosing between this and props

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

An imperative change lasts until the reconciler sets the same thing from the props: a paint or layout property 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, which sets only the values that changed. A change to any other prop leaves it 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 the rendered props as the source of truth.

Summary

Functions

Animates the camera along a straight path, without the zoom-out arc.

Fits the camera to a bounding box.

Animates the camera along a curved flight path.

Moves the camera with no animation at all.

Recomputes the map's size after its container has been resized.

Attaches feature state, the mechanism behind hover and selection styling.

Sets a layer's filter, or clears it when filter is nil.

Sets one layout property on a layer.

Sets one paint property on a layer.

Replaces the whole style.

Functions

ease_to(cid, opts)

@spec ease_to(
  String.t(),
  keyword()
) :: :ok

Animates the camera along a straight path, without the zoom-out arc.

Takes the same options as fly_to/2 minus the flight-specific :speed and :curve, which it ignores.

fit_bounds(cid, bounds, opts \\ [])

@spec fit_bounds(String.t(), term(), keyword()) :: :ok

Fits the camera to a bounding box.

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

HoloMap.API.fit_bounds("explorer", [-71.0, 19.0, -70.0, 20.0], padding: 48)

fly_to(cid, opts)

@spec fly_to(
  String.t(),
  keyword()
) :: :ok

Animates the camera along a curved flight path.

Accepts :center, :zoom, :bearing, :pitch, :duration, :easing, :speed, :curve, :offset, :padding, :max_duration, :essential and :animate.

HoloMap.API.fly_to("explorer", center: {-70.66, 19.45}, zoom: 14, duration: 1_200)

Easing

:easing shapes the animation's progress over its duration. It takes one of CSS's timing curves by name, or a cubic Bézier curve:

ValueCurve
:linearConstant speed
:ease_inStarts slowly, CSS's ease-in: cubic-bezier(0.42, 0, 1, 1)
:ease_outEnds slowly, CSS's ease-out: cubic-bezier(0, 0, 0.58, 1)
:ease_in_outStarts and ends slowly, CSS's ease-in-out: cubic-bezier(0.42, 0, 0.58, 1)
{:cubic_bezier, x1, y1, x2, y2}The curve CSS's cubic-bezier(x1, y1, x2, y2) describes. x1 and x2 are times and lie between 0 and 1; y1 and y2 may leave that range, for a move that overshoots
HoloMap.API.ease_to("explorer", bearing: 90, duration: 900, easing: :ease_in_out)

Left out, MapLibre eases the move its own way, along cubic-bezier(0.25, 0.1, 0.25, 1). Any other value raises ArgumentError: MapLibre takes the easing as a JavaScript function, which cannot be sent from Elixir, so the browser runtime builds it from one of these.

jump_to(cid, opts)

@spec jump_to(
  String.t(),
  keyword()
) :: :ok

Moves the camera with no animation at all.

Takes the same options as fly_to/2. Those that shape an animation, such as :duration and :easing, have no effect, though an :easing that is not one of the documented values still raises.

remove_feature_state(cid, feature, key \\ nil)

@spec remove_feature_state(String.t(), map(), String.t() | nil) :: :ok

Clears feature state.

Omit key to clear every state entry on the feature.

resize(cid)

@spec resize(String.t()) :: :ok

Recomputes the map's size after its container has been resized.

Almost never needed. MapLibre observes its own container, so a map whose container changes size follows along by itself and calling this does nothing. That holds for CSS transitions, flex reflows and window resizes alike.

The one case where it is required is a map built with track_resize={false}, which switches that observer off. The canvas then keeps its old size until this is called:

<HoloMap.Map cid="panel" style={@style} width={@width} track_resize={false} />

def action(:panel_resized, _params, component) do
  HoloMap.API.resize("panel")
  component
end

set_feature_state(cid, feature, state)

@spec set_feature_state(String.t(), map(), map()) :: :ok

Attaches feature state, the mechanism behind hover and selection styling.

feature identifies the feature: %{source: "parcels", id: 12}, plus :source_layer for a vector source.

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

set_filter(cid, layer_id, filter)

@spec set_filter(String.t(), String.t(), list() | nil) :: :ok

Sets a layer's filter, or clears it when filter is nil.

HoloMap.API.set_filter("explorer", "parcels-fill", ["==", ["get", "zone"], "R1"])

set_layout_property(cid, layer_id, property, value)

@spec set_layout_property(String.t(), String.t(), atom() | String.t(), term()) :: :ok

Sets one layout property on a layer.

Useful for toggling visibility without removing the layer:

HoloMap.API.set_layout_property("explorer", "parcels-fill", :visibility, "none")

set_paint_property(cid, layer_id, property, value)

@spec set_paint_property(String.t(), String.t(), atom() | String.t(), term()) :: :ok

Sets one paint property on a layer.

property may be given as an atom in Elixir style (:fill_color) or as the specification name ("fill-color").

set_style(cid, style, opts \\ [])

@spec set_style(String.t(), String.t() | map(), keyword()) :: :ok

Replaces the whole style.

Prefer HoloMap.Map's style prop. Either way MapLibre diffs the new style against the one on the map, and the sources, layers, images, terrain and sky the components declared stay on the map, so only the basemap changes. HoloMap.Map's Changing the style describes what is kept, and when MapLibre builds the new style from scratch instead. Calls made while the new style loads wait for it.

HoloMap.API.set_style("explorer", "https://tiles.example.com/dark.json")

Options

  • :diff - false asks MapLibre to build the new style from scratch: the map starts from an empty style, and the reconciler declares the sources, layers, images, terrain and sky it owns again once the new style has loaded. Their feature state is cleared. Defaults to true.

Any other option, or a :diff that is not a boolean, raises ArgumentError.