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)
endThe 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
| Situation | Use |
|---|---|
| The camera reflects application state | HoloMap.Map's center/zoom props |
| The camera should animate in response to an event | fly_to/2, ease_to/2 |
| A layer's paint reflects state | HoloMap.Layer.*'s paint prop |
| A one-off highlight your state does not track | set_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.
Clears feature state.
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
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.
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)
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:
| Value | Curve |
|---|---|
:linear | Constant speed |
:ease_in | Starts slowly, CSS's ease-in: cubic-bezier(0.42, 0, 1, 1) |
:ease_out | Ends slowly, CSS's ease-out: cubic-bezier(0, 0, 0.58, 1) |
:ease_in_out | Starts 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.
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.
Clears feature state.
Omit key to clear every state entry on the feature.
@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
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})
Sets a layer's filter, or clears it when filter is nil.
HoloMap.API.set_filter("explorer", "parcels-fill", ["==", ["get", "zone"], "R1"])
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")
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").
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-falseasks 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 totrue.
Any other option, or a :diff that is not a boolean, raises
ArgumentError.