MapLibreGL
The root map component. Renders a MapLibre GL JS map and yields pre-bound child components for adding sources, layers, markers, popups, controls, and event listeners.
The block is only rendered after the map has fully loaded.
Import
import MapLibreGL from 'ember-maplibre-gl/components/maplibre-gl';Example
<MapLibreGL
@initOptions={{hash style="https://demotiles.maplibre.org/style.json" center=(array 0 0) zoom=1}}
@mapLoaded={{this.onMapLoaded}}
as |map|
>
<map.source @options={{this.geojsonSource}} as |source|>
<source.layer @options={{this.circleLayer}} />
</map.source>
<map.marker @lngLat={{this.markerPosition}} />
<map.on @event="click" @action={{this.onClick}} />
</MapLibreGL>Signature
interface MapLibreGLSignature {
Element: HTMLDivElement;
Args: {
/**
* MapLibre map options (style, center, zoom, etc.). Passed once at construction; later changes are ignored.
* The `container` property is managed internally and should be omitted.
*
* To change the style later, call `map.setStyle()` on the instance. The sources, layers and
* images declared in the block are added again when the new style has loaded.
*
* @see https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/MapOptions/
*/
initOptions: MapInitOptions;
/** Called once the map's style and tiles have loaded. Receives the map instance. */
mapLoaded?: (map: MaplibreMap) => void;
/**
* Cache the WebGL map instance on teardown and reuse it on remount.
* Avoids expensive context creation on repeated route transitions.
* Only works when `initOptions.style` is a URL string.
*/
reuseMaps?: boolean;
/** Override the map constructor (e.g. for testing or mapbox-gl compatibility). */
mapLib?: new (...args: unknown[]) => MaplibreMap;
};
Blocks: {
/**
* Yields an object with pre-bound child components and the map instance.
* Available after the map has loaded.
*/
default: [
{
/** Invoke a method on the map instance declaratively. */
call: WithBoundArgs<typeof MapLibreGLCall, 'obj'>;
/** Add a UI control (navigation, scale, etc.) to the map. */
control: WithBoundArgs<typeof MapLibreGLControl, 'map' | 'parent'>;
/** Load and register a custom image for use in symbol layers. */
image: WithBoundArgs<typeof MapLibreGLImage, 'map' | 'parent'>;
/** Add a rendering layer directly (without an explicit source component). */
layer: WithBoundArgs<typeof MapLibreGLLayer, 'map' | 'parent'>;
/** Place a draggable marker on the map. */
marker: WithBoundArgs<typeof MapLibreGLMarker, 'map' | 'parent'>;
/** Bind an event listener to the map. */
on: WithBoundArgs<typeof MapLibreGLOn, 'eventSource'>;
/** Show a popup overlay on the map. */
popup: WithBoundArgs<typeof MapLibreGLPopup, 'map'>;
/** Add a data source (GeoJSON, vector tiles, etc.) to the map. */
source: WithBoundArgs<typeof MapLibreGLSource, 'map' | 'parent'>;
/** The underlying MapLibre map instance (always defined inside the default block). */
instance: MaplibreMap | undefined;
/** The Ember component instance (useful for associateDestroyableChild). */
component: MapLibreGL;
}
];
/** Yielded when the map encounters a fatal error (e.g. WebGL context lost). Receives an `Error` with a `.message` property. */
error: [Error];
};
}Args
| Arg | Type | Required | Description |
|---|---|---|---|
initOptions | MapInitOptions | Yes | MapLibre map options (style, center, zoom, etc.). Passed once at construction; later changes are ignored. The container property is managed internally and should be omitted. To change the style later, call map.setStyle() on the instance. The sources, layers and images declared in the block are added again when the new style has loaded. |
mapLoaded | Function | No | Called once the map's style and tiles have loaded. Receives the map instance. |
reuseMaps | boolean | No | Cache the WebGL map instance on teardown and reuse it on remount. Avoids expensive context creation on repeated route transitions. Only works when initOptions.style is a URL string. |
mapLib | MapConstructor | No | Override the map constructor (e.g. for testing or mapbox-gl compatibility). |
MapInitOptions
MapLibre MapOptions ↗ without container, which is managed internally.
type MapInitOptions = Omit<MapOptions, 'container'>Yields
| Property | Type | Description |
|---|---|---|
call | MapLibreGLCall | Invoke a method on the map instance declaratively. |
control | MapLibreGLControl | Add a UI control (navigation, scale, etc.) to the map. |
image | MapLibreGLImage | Load and register a custom image for use in symbol layers. |
layer | MapLibreGLLayer | Add a rendering layer directly (without an explicit source component). |
marker | MapLibreGLMarker | Place a draggable marker on the map. |
on | MapLibreGLOn | Bind an event listener to the map. |
popup | MapLibreGLPopup | Show a popup overlay on the map. |
source | MapLibreGLSource | Add a data source (GeoJSON, vector tiles, etc.) to the map. |
instance | `MaplibreMap | undefined` |
component | MapLibreGL | The Ember component instance (useful for associateDestroyableChild). |
Demo
import MapLibreGL from 'ember-maplibre-gl/components/maplibre-gl';
const options = {
style: 'https://tiles.openfreemap.org/styles/bright',
center: [12.5, 41.88],
zoom: 5,
};
<template>
<MapLibreGL
@initOptions={{options}}
style="height: 300px; width: 100%; border-radius: 8px;"
/>
</template>Error Handling
The component supports an error named block. The yielded value is an Error object with a .message property:
<MapLibreGL @initOptions={{this.options}}>
<:default as |map|>
<!-- map content -->
</:default>
<:error as |error|>
<p>Map failed to load: {{error.message}}</p>
</:error>
</MapLibreGL>Changing the style
@initOptions is read once, so a new style there does nothing. Call setStyle on the instance:
<MapLibreGL @initOptions={{this.mapOptions}} as |map|>
<map.source @options={{this.points}} as |source|>
<source.layer @options={{this.circles}} />
</map.source>
<button type="button" {{on "click" (fn this.useStyle map.instance this.darkStyle)}}>Dark</button>
</MapLibreGL>useStyle = (map: Map, style: string) => map.setStyle(style);MapLibre drops the sources, layers and images that were added at runtime when the style changes. <map.source>, <source.layer>, <map.layer> and <map.image> add theirs again once the new style has loaded, in template order. Markers, popups and controls are not part of the style and stay where they are.
- With a diffed
setStyle()(the default), this needs maplibre-gl 5.16 or later, the first version that firesstyle.loadon a diff. - If the new style does not have the layer that
@beforenames, the layer is added on top and a warning is logged. - An image
@namemust not be an image id in the style's sprite, and a source or layer id must not be one in the new style. Otherwise the style's own is kept, and removed when the component is destroyed. - With
@reuseMaps, a map whose style was changed is destroyed on teardown, not pooled: the pool is keyed by the style the map was built with. A map that is still fetching a diffedsetStyle(url)is seen only from maplibre-gl 5.22. A restored WebGL context loads the style again and counts as a change too. AsetStyle()before the map's firstloadis not seen, so such a map is pooled under its first style.
Testing
await render() and await visit() wait for the map's load event, and for images that <map.image> loads. await settled() after map.setStyle() waits until the new style has loaded and the block has added its sources, layers and images again. The new style's sprite can still be loading. Waiting for a diffed setStyle(url) while it is fetched needs maplibre-gl 5.22 or later. Assertions about those need no waitUntil.
Sources, layers, markers and popups need no waiting either, but for a different reason: they act on the map while the block renders, so await render() already covers them. What is still yours to wait for is anything that happens after that — a popup opened by a marker click, or tile loading.
A wait ends when:
- the map loads, the image is added, or the style from
setStyle()has loaded (asetStyle()that changes nothing ends it at once) - the map cannot load: its constructor throws (no WebGL), or an
errorarrives before the style has loaded. Theerrorblock then renders. - a style from
setStyle()fails: anerrorarrives before it has loaded. Withdiff: falsethe old style is already removed, so the map stays blank until the nextsetStyle(). - a newer image load replaces one still in flight
- the component is destroyed
- 10 seconds pass, which prints a console warning. This covers a request that never answers.
A stub constructor passed to @mapLib must fire load, or each test waits 10 seconds. The waiters and their timers live in development builds, which includes test builds. A production build has neither.
Custom Map Constructor
The @mapLib arg lets you swap in a different map constructor. This is useful for:
- Testing — pass a mock/stub constructor to avoid WebGL in tests
- Mapbox GL JS — in theory, Mapbox's
Mapclass could work since MapLibre forked from it, but API drift means this is not guaranteed - Custom subclasses — extend
maplibregl.Mapwith project-specific behavior
import MapLibreGL from 'ember-maplibre-gl/components/maplibre-gl';
import { Map as CustomMap } from 'some-maplibre-fork';
const options = { style: '...', center: [0, 0], zoom: 2 };
<template>
<MapLibreGL @initOptions={{options}} @mapLib={{CustomMap}} />
</template>WARNING
The constructor must be API-compatible with maplibregl.Map. Passing an incompatible constructor will likely cause runtime errors in sub-components that call MapLibre-specific methods.