HoloMap.Map (HoloMap v0.2.0)

Copy Markdown View Source

The map container. Everything else in HoloMap goes inside one.

<HoloMap.Map cid="explorer" style={@style_url} center={{-70.66, 19.45}} zoom={11}>
  <HoloMap.Source.GeoJSON id="parcels" data={@parcels} />
  <HoloMap.Layer.Fill id="parcels-fill" source="parcels" paint={%{fill_color: "#3b82f6"}} />
</HoloMap.Map>

The cid is the map's identity

cid is Hologram's component id, and HoloMap uses it as the map's name throughout: the DOM container is hm--<cid>, the browser registry is keyed by it, and it is the first argument to every HoloMap.API function. One identifier, no second id prop to keep in sync.

A cid is mandatory. Hologram only runs init/3, and therefore only allows actions, on stateful components, and the map needs an action to boot.

Sizing

A map with no height renders as a zero-pixel box, which is the single most common way to end up staring at a blank page. height defaults to "400px" for that reason. Pass class instead when the size comes from your own CSS, and set height={nil} to opt out of the inline style entirely.

The worker URL

MapLibre parses tiles in a Web Worker that it starts from maplibre-gl-worker.mjs, resolved relative to its own module URL. Hologram bundles MapLibre into a page bundle under /hologram/, so that resolution lands on a file which is not there. The symptom is a map that renders its background and never draws a single tile. MapLibre reports the failed worker through the map's error event, so on_error receives it, with the message "Worker failed to load. Check that the worker URL is correct.". A map without on_error leaves MapLibre to log it to the browser console.

HoloMap sets the URL explicitly to avoid that. The default assumes the application serves maplibre-gl-worker.mjs from /assets/; the Installation guide has the copy step, and worker_url overrides the path if the worker lives elsewhere.

The default URL carries the version of the MapLibre the page loaded as a v query parameter, /assets/maplibre-gl-worker.mjs?v=6.19.0 on 6.19.0, as MapLibre's own default has since 6.13.0. Upgrading maplibre-gl therefore changes the URL, and no cache can hand the new library the old worker. Plug.Static ignores the query and serves the file; a CDN in front of it has to keep the query in its cache key. A worker_url you set is used as it is, with nothing added: put a version or a content hash in it, or serve the file with headers that make browsers revalidate it, as Plug.Static's default ETag without a max-age does.

MapLibre keeps one worker URL per page, and the first map to boot sets it. A later map that asks for another one, its own worker_url or the default, runs on the page's worker, and the browser runtime logs a warning once for each other URL. Give every map on a page the same worker_url.

Declarative camera versus animated camera

center, zoom, bearing and pitch are state. Changing one jumps the camera there, and panning the map by hand does not write back into them: the props describe where the application thinks the camera should be, not where the user dragged it. Nothing re-renders while the user interacts, so there is no fight between the two.

When a camera move is an event rather than a value, such as flying to a search result, use HoloMap.API.fly_to/2 and leave the props alone.

The limits, min_zoom, max_zoom, min_pitch, max_pitch and max_bounds, are state as well. A change that leaves the camera outside the new limits makes MapLibre zoom, pitch or pan it back within them. Like a jump, that move is not reported: on_move_start, on_move_end, on_zoom_end and on_pitch_end do not fire for it. The camera props keep the values the application gave them, and the next move the user makes reports where the limits left the camera.

A limit set back to nil is lifted, and the camera is as free as on a map built without it: no bounds, a zoom from -2 to 22 and a pitch from 0 to 60, MapLibre's defaults. A camera prop set to nil is different: it leaves the camera where it is.

render_world_copies and pixel_ratio apply as they change too, and set back to nil they return to MapLibre's defaults: world copies drawn, and the device's pixel ratio. A new pixel_ratio resizes the canvas, which MapLibre reports as a move, and that move is not reported either.

Interaction

interactive, cooperative_gestures and the seven handler props, drag_rotate, drag_pan, scroll_zoom, double_click_zoom, box_zoom, keyboard and touch_zoom_rotate, apply as they change, so a "lock the map" toggle can be state:

<HoloMap.Map cid="explorer" style={@style} interactive={!@locked} />

interactive={false} turns off every handler MapLibre's interactive option covers: the seven above and MapLibre's touchPitch, which options reaches. It also takes away the grab cursor over the map and the canvas's place in the tab order, as MapLibre does for a map built that way. Set back to true or nil, it turns on again each of those handlers whose own prop is not false. A handler prop that changes while the map is not interactive applies once it is. cooperative_gestures stands apart, as in MapLibre: interactive leaves it as it is.

Changing the style

style applies as it changes, compared by value, so an inline style rendered again unchanged does nothing. MapLibre diffs the new style against the one on the map, and the browser runtime carries every source and layer the components declared into the new style, so only the basemap changes: the map does not go blank, sources both styles share keep their tiles, and a sprite and glyphs both styles use are not requested again. The components' layers keep their template order, a before_id names a layer of the new style, and each layer goes back to its props, losing whatever HoloMap.API set on it. Images, terrain, sky and the feature state of the components' sources stay as they were. A new style the diff finds nothing to change in leaves the map as it is, and one that fails MapLibre's validation is reported through on_error, or the browser console, and the map keeps the style it has.

A source or layer whose id the new style uses is the new style's, as on the first style: its component adds nothing, the layers of such a source stay off the map, and the browser runtime logs a warning naming them.

MapLibre builds the new style from scratch instead when it cannot diff it, for example when the new style sets centerAltitude, and when the change comes before the first style has loaded. The map then starts from an empty style, and the components' sources, layers, images, terrain and sky are declared again once it has loaded. HoloMap.API.set_style/3 with diff: false asks for that on purpose. Either way, changes and HoloMap.API calls made while the style changes wait for it.

Props read only when the map is built

MapLibre reads some options only while it builds the map, and has no setter to change them afterwards. The props table below marks them build only: hash, attribution_control, antialias, pitch_with_rotate, fade_duration, bearing_snap, track_resize, max_tile_cache_size, local_ideograph_font_family, validate_style, locale and worker_url. A change to one leaves the map as it was built, and the browser runtime logs a console warning that names the MapLibre option, once for each option on each map. A key of options is build only as well, unless it is the MapLibre name of an option that applies as it changes, such as "maxBounds", which then applies as its prop does.

Props

PropTypeAfter the map is builtNotes
stylestring or mapAppliesRequired. Style URL or inline style object. See Changing the style
center{lng, lat}AppliesCamera centre
zoom / bearing / pitchnumberAppliesCamera attitude
min_zoom / max_zoom / min_pitch / max_pitchnumberAppliesCamera constraints. nil lifts one
max_boundsboundsAppliesPan constraint, in any shape HoloMap.Spec.bounds/1 accepts. nil lifts it
interactivebooleanAppliesfalse for a static, non-navigable map. See Interaction
hashbooleanBuild onlySync the camera to the URL fragment
attribution_controlbooleanBuild onlyMapLibre's built-in attribution. See HoloMap.Control.Attribution to place it yourself
cooperative_gesturesbooleanAppliesRequire ctrl/⌘ to scroll-zoom, so the page still scrolls
drag_rotate / drag_pan / scroll_zoom / double_click_zoom / box_zoom / keyboard / touch_zoom_rotatebooleanAppliesIndividual interaction handlers
antialiasbooleanBuild onlyAsk the WebGL context for antialiasing, which smooths the edges of fill-extrusion layers. Sent as MapLibre's canvasContextAttributes
render_world_copiesbooleanAppliesDraw the world again beyond 180 degrees of longitude. Default true
pixel_rationumberAppliesRender at this ratio of canvas pixels to CSS pixels instead of the device's
fade_duration / bearing_snap / track_resize / pitch_with_rotate / max_tile_cache_size / local_ideograph_font_family / validate_style / localevariousBuild onlyPassed straight through to MapLibre
optionsmapBuild only, but see aboveRaw MapLibre constructor options, merged last. Keys must be MapLibre's own camelCase names: options={%{"maxCanvasSize" => [2048, 2048]}}. A "canvasContextAttributes" map given here is merged with antialias, and its keys win
worker_urlstringBuild onlyWhere the application serves maplibre-gl-worker.mjs. Default "/assets/maplibre-gl-worker.mjs?v=<MapLibre version>". See The worker URL
classstringAppliesCSS classes for the wrapper element
height / widthstringAppliesInline size for the canvas. nil opts out and leaves sizing to CSS

Event props

on_load, on_click, on_dbl_click, on_context_menu, on_mouse_move, on_move_start, on_move_end, on_zoom_end, on_rotate_end, on_pitch_end, on_idle, on_error.

Events

Handlers receive a payload with center, zoom, bearing, pitch and bounds, plus lng_lat and point for pointer events, and error for on_error: a map with the message, plus the status and url of a failed request and the source_id of a source's error when MapLibre reports them. The one exception is a map MapLibre could not build, below.

def action(:map_moved, params, component) do
  put_state(component, :bounds, params.bounds)
end

See HoloMap.Event for the handler syntax and the Events guide for the full payload reference.

A map MapLibre cannot build

MapLibre draws with WebGL2. On a browser or device that cannot create a WebGL2 context, it cannot build the map at all, and the container stays empty. on_error receives that failure once, with MapLibre's message, "WebGL2 is required to display this map. ...", and its payload carries error alone: there is no camera to read center, zoom, bearing, pitch or bounds from. Camera limits MapLibre refuses when it builds the map, such as a min_zoom above max_zoom, are reported the same way. A map without on_error logs the error to the browser console.

The map is not built again while its container stays on the page, and the other maps on the page are built as usual. HoloMap.API calls made for it are dropped at once, with a console warning that names MapLibre's error, the ones queued for its boot among them. Telling the failure apart from the errors of a map that was built takes a match on the camera:

def action(:map_failed, %{center: _center} = params, component) do
  put_state(component, :notice, "The map reported: " <> params.error.message)
end

def action(:map_failed, params, component) do
  put_state(component, :notice, "This browser cannot show the map. " <> params.error.message)
end

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.