# MapAdapter

The map engine, seen from the ui. The MapLibre adapter implements this in
its own module, and nothing outside that module may name an engine class,
event or DOM node. The port is the boundary between the ui and the engine,
and lets the ui run against an in-memory fake in tests.

This is an interface exported by `@mapcairn/core`, checked against version 0.2.0.

## Signature

```ts
import type { MapAdapter } from "@mapcairn/core";
 
interface MapAdapter
```

## Methods

### destroy

```ts
destroy(): void;
```

Removes the engine and every listener it added. Safe to call twice.

### featuresAt

```ts
featuresAt(point: PixelPoint): readonly MapFeatureHit[];
```

Every tappable feature of the data layers at a pixel of the map (a
fingertip's reach for points and lines, exactly for polygons), topmost
first, each listed once. Empty when nothing is there or the map is not
ready. The ui calls it for the keyboard route to the list a multi-feature
tap opens.

- `point` (`PixelPoint`)

### fitBounds

```ts
fitBounds(bounds: Bounds, options?: MapFitOptions): void;
```

Moves and zooms so the box fills the map, inside the padding, and not past `maxZoom` when that is given.

- `bounds` (`Bounds`)
- `options?` (`MapFitOptions`)

### focus

```ts
focus(): void;
```

Moves keyboard focus to the map host.

### getBounds

```ts
getBounds(): Bounds;
```

The box of latitudes and longitudes the map shows now, covering a rotated map.

### getMaxZoom

```ts
getMaxZoom(): number;
```

The deepest normalised zoom the active basemap allows.

### getMinZoom

```ts
getMinZoom(): number;
```

Normalised zoom limits of the active basemap.

### getView

```ts
getView(): ViewState;
```

The view the map shows now. It is updated as the map moves.

### isVisible

```ts
isVisible(latlng: LatLng): boolean;
```

Whether a place is on the side of the map the person can see. Always true
on a flat map. On a globe it is false for a place beyond the horizon,
where `project` still returns a pixel that must not be drawn.

- `latlng` (`LatLng`)

### metresPerPixel

```ts
metresPerPixel(lat?: number): number;
```

Ground resolution at a latitude (defaults to the view centre) at the
current zoom. On a globe it is the view centre's resolution wherever
asked, because the sphere has one scale (`mapMetresPerPixel`).

- `lat?` (`number`)

### mount

```ts
mount(host: MapHostElement, options: MapMountOptions): Promise<void>;
```

Creates the engine inside `host`. Rejects with a `MapEngineError` when the
engine cannot start (for example, no WebGL).

- `host` (`MapHostElement`)
- `options` (`MapMountOptions`)

### on

```ts
on<K extends keyof MapEvents>(event: K, handler: (payload: MapEvents[K]) => void): () => void;
```

Subscribes to an event. Returns the unsubscribe function.

- `event` (`K`)
- `handler` (`(payload: MapEvents[K]) => void`)

### panBy

```ts
panBy(dx: number, dy: number, options?: MapMoveOptions): void;
```

Moves the view by a number of screen pixels (positive x is right, positive y is down).

- `dx` (`number`)
- `dy` (`number`)
- `options?` (`MapMoveOptions`)

### project

```ts
project(latlng: LatLng): PixelPoint;
```

The pixel of a position on the map host. On a flat map the longitude
names the copy of the world: 190 is one world east of -170, so the ui
picks the copy it draws (`placePoint`, `placePath`).

- `latlng` (`LatLng`)

### removeLayer

```ts
removeLayer(id: string): void;
```

Removes whatever is drawn under `id`. Does nothing for an unknown id.

- `id` (`string`)

### resetBearing

```ts
resetBearing(options?: MapMoveOptions): void;
```

Sets the bearing to north.

- `options?` (`MapMoveOptions`)

### rotateBy

```ts
rotateBy(degrees: number, options?: MapMoveOptions): void;
```

Rotates by degrees clockwise.

- `degrees` (`number`)
- `options?` (`MapMoveOptions`)

### setAnimationEnabled

```ts
setAnimationEnabled(enabled: boolean): void;
```

Turns every engine animation on or off (reduced motion).

- `enabled` (`boolean`)

### setBasemap

```ts
setBasemap(basemap: BasemapDefinition): Promise<void>;
```

Swaps the basemap, keeping the view. Resolves once the new basemap's
style has loaded. If it cannot load, rejects with a `MapEngineError` and
leaves the previous basemap and its zoom limits in place, so the map
keeps working.

- `basemap` (`BasemapDefinition`)

### setEditHandles

```ts
setEditHandles(handles: readonly MapEditHandle[]): void;
```

Replaces the handles the person can press and drag. A press within reach
of a handle (12 px for a mouse, 22 px for a finger, the nearest wins) is
reported as `handleDrag` and does not pan the map or produce a `click`.
An empty list turns handle gestures off. Handles belong to the adapter,
not to the style, so they survive `setBasemap`. A press already in
progress carries on with the handle it began on.

- `handles` (`readonly MapEditHandle[]`)

### setFeatureLayer

```ts
setFeatureLayer(id: string, features: readonly MapFeature[]): void;
```

Draws a feed layer's points under `id`, replacing what was drawn under
that id. Data layers are drawn above the basemap in a fixed order: tile
overlays lowest, then shapes, then points. They survive `setBasemap`, and
may be set before `mount`, which draws them once the map has loaded.

- `id` (`string`)
- `features` (`readonly MapFeature[]`)

### setHeatmapLayer

```ts
setHeatmapLayer(id: string, heatmap: MapHeatmap): void;
```

Draws point density as a heatmap under `id`, replacing what was drawn
under that id. A heatmap sits above styled shapes and below points.

- `id` (`string`)
- `heatmap` (`MapHeatmap`)

### setKeyboardEnabled

```ts
setKeyboardEnabled(enabled: boolean): void;
```

Turns the engine's own keyboard handling on or off.

- `enabled` (`boolean`)

### setProjection

```ts
setProjection(projection: MapProjection): void;
```

Draws the map flat or as a globe. The adapter applies it again after every
style load, so a basemap swap keeps it and a style's own request for a
different shape is ignored.

- `projection` (`MapProjection`)

### setShapeLayer

```ts
setShapeLayer(id: string, shapes: MapShapes): void;
```

Draws polygons and lines under `id`, replacing what was drawn under that id.

- `id` (`string`)
- `shapes` (`MapShapes`)

### setStyledShapes

```ts
setStyledShapes(id: string, layer: MapStyledShapes): void;
```

Draws polygons or lines with a look each under `id`, replacing what was
drawn under that id. They sit between tile overlays and points, and
survive `setBasemap` like the other data layers.

- `id` (`string`)
- `layer` (`MapStyledShapes`)

### setTileLayer

```ts
setTileLayer(id: string, overlay: MapTileOverlay): void;
```

Draws or updates a tile overlay under `id`. An opacity change alone costs no request.

- `id` (`string`)
- `overlay` (`MapTileOverlay`)

### setVectorLayer

```ts
setVectorLayer(id: string, layer: MapVectorTiles): void;
```

Draws a vector tile layer under `id`, replacing what was drawn under that
id. Its features are not tappable, because the ui never receives them.

- `id` (`string`)
- `layer` (`MapVectorTiles`)

### setView

```ts
setView(view: Partial<ViewState>, options?: MapMoveOptions): void;
```

Moves the map to the parts of the view that are given and keeps the rest. A move may animate unless animation is off.

- `view` (`Partial<ViewState>`)
- `options?` (`MapMoveOptions`)

### toImage

```ts
toImage(options?: MapImageOptions): Promise<Blob>;
```

Draws the map with its data layers as a PNG.

With no `size`, `pixelRatio` or `view`, it copies the map as it is on
screen and never requests anything the map has not already loaded.

With any of them, it draws the requested size and view on a map of its own
and leaves the map on screen where it is: no move, no event, no repaint.
That map is separate, with its own caches, so it fetches again what its
view needs from the providers the map on screen already uses: the style's
tile descriptions, sprite sheets and glyph ranges, and the tiles of its
view, unless the browser's HTTP cache has them. It adds no source and no
origin of its own. It waits for the tiles to load. A request over the size
limit is refused before anything is drawn. Requested images are drawn one
at a time, each waiting for the ones asked for before it; the queue has no
cap and no way for a caller to cancel yet. An export that waits in the
queue while the basemap, a data layer or the projection changes fails with
`export-failed` ("The map changed while the image was waiting.") rather
than mixing the new style with the old view and credits. The CSS size is
rounded to whole pixels, and a box must have its west below its east, so
one across the antimeridian has its east past 180.

Rejects with a `MapEngineError` whose reason is `export-failed` when the
engine cannot produce an image, and also while a basemap swap is still
loading, so a half-swapped map is never saved. That is checked again just
before the pixels are copied, so a swap that is still loading then is
caught; one that starts and finishes during the wait is not noticed, and
the image shows the basemap the map had when the export started.
A requested size or view fails with a [`MapImageExportError`](https://docs.mapcairn.com/api/core/MapImageExportError.md), which is
one of those, when the request is not valid (`invalid`), is too large
(`too-large`) or its tiles do not finish loading in time (`timed-out`).

Known limits: a second map that cannot start (the likeliest cause is
memory pressure, on iOS especially) is an `export-failed` error without a
`kind`; and a tile that errors (a 404, a 429, a blocked response) still
lets the map settle, so the image is saved with a gap and no error.

- `options?` (`MapImageOptions`)

### unproject

```ts
unproject(point: PixelPoint): LatLng;
```

The position under a pixel, with its longitude in [-180, 180).

- `point` (`PixelPoint`)

### zoomIn

```ts
zoomIn(options?: MapMoveOptions): void;
```

Zooms in by one level, towards the centre of the map.

- `options?` (`MapMoveOptions`)

### zoomOut

```ts
zoomOut(options?: MapMoveOptions): void;
```

Zooms out by one level, from the centre of the map.

- `options?` (`MapMoveOptions`)

## Remarks

Keyboard handling and long-press are implemented once in the ui and
 call the methods below. [`MapAdapter`](https://docs.mapcairn.com/api/core/MapAdapter.md) turns the
 engine's own key handling off for that reason.

## See also

- [`PixelPoint`](https://docs.mapcairn.com/api/core/PixelPoint.md)
- [`MapFeatureHit`](https://docs.mapcairn.com/api/core/MapFeatureHit.md)
- [`Bounds`](https://docs.mapcairn.com/api/core/Bounds.md)
- [`MapFitOptions`](https://docs.mapcairn.com/api/core/MapFitOptions.md)
- [`ViewState`](https://docs.mapcairn.com/api/core/ViewState.md)
- [`LatLng`](https://docs.mapcairn.com/api/core/LatLng.md)
- [`MapHostElement`](https://docs.mapcairn.com/api/core/MapHostElement.md)
- [`MapMountOptions`](https://docs.mapcairn.com/api/core/MapMountOptions.md)
- [`MapEvents`](https://docs.mapcairn.com/api/core/MapEvents.md)
- [`MapMoveOptions`](https://docs.mapcairn.com/api/core/MapMoveOptions.md)
- [`BasemapDefinition`](https://docs.mapcairn.com/api/core/BasemapDefinition.md)
- [`MapEditHandle`](https://docs.mapcairn.com/api/core/MapEditHandle.md)
- [`MapFeature`](https://docs.mapcairn.com/api/core/MapFeature.md)
- [`MapHeatmap`](https://docs.mapcairn.com/api/core/MapHeatmap.md)
- [`MapProjection`](https://docs.mapcairn.com/api/core/MapProjection.md)
- [`MapShapes`](https://docs.mapcairn.com/api/core/MapShapes.md)
- [`MapStyledShapes`](https://docs.mapcairn.com/api/core/MapStyledShapes.md)
- [`MapTileOverlay`](https://docs.mapcairn.com/api/core/MapTileOverlay.md)
- [`MapVectorTiles`](https://docs.mapcairn.com/api/core/MapVectorTiles.md)
- [`MapImageOptions`](https://docs.mapcairn.com/api/core/MapImageOptions.md)

Source: [packages/core/src/ports/map.ts:554](https://github.com/MapCairn/mapcairn/blob/main/packages/core/src/ports/map.ts#L554)
