@viamrobotics/prime-map
@viamrobotics/prime-map is a set of MapLibre maps for Svelte 5. A MapLibre fills its parent and hands the map to the markers, controls, and layers inside it.
MapLibre, a map on OpenStreetMap or Google Maps tiles, with a reactive context for its children.MapLibreMarkerandDirectionalMarker, a pin and a heading arrow.CenterControls,FollowControls,NavigationControls, andSatelliteControls.LngLatInput, a latitude and longitude pair of fields.- Mercator and Cartesian conversions between longitude and latitude and meters.
- A Three.js bridge on
@viamrobotics/prime-map/threethat renders a scene on the map.
Installation
Section titled “Installation”-
Install the package and its peers. The
sveltepeer is^5.29.0and themaplibre-glpeer is^6.0.0. Addthreeonly for the./threesubpath.Terminal window pnpm add @viamrobotics/prime-map @viamrobotics/prime-ui @viamrobotics/prime-controls @viamrobotics/tailwind-config maplibre-glTerminal window npm install @viamrobotics/prime-map @viamrobotics/prime-ui @viamrobotics/prime-controls @viamrobotics/tailwind-config maplibre-glTerminal window yarn add @viamrobotics/prime-map @viamrobotics/prime-ui @viamrobotics/prime-controls @viamrobotics/tailwind-config maplibre-gl -
Import the stylesheets once, after Tailwind.
MapLibreimports maplibre-gl’s own stylesheet.@import "tailwindcss";@import "@viamrobotics/prime-ui/styles.css";@import "@viamrobotics/prime-controls/styles.css";@import "@viamrobotics/prime-map/styles.css";
MapLibre fills its parent, so give the parent a size. bind:center and bind:zoom follow the camera, bind:satellite follows the satellite view, and bind:map gives the maplibre-gl Map. Its children mount once the map loads its first style. A child reads the map through useMapLibre(), whose map, center, zoom, maxZoom, size, mapProvider, apiKey, and satellite are reactive, and listens to a map event with useMapLibreEvent(event, listener). Both throw outside a MapLibre.
Markers
Section titled “Markers”MapLibreMarker draws maplibre-gl’s pin, or an element you pass. DirectionalMarker draws an arrow that points along rotation, clockwise in degrees.
Code
<script lang="ts"> import { DirectionalMarker, MapLibre, MapLibreMarker, } from "@viamrobotics/prime-map"; import { LngLat } from "maplibre-gl";
let heading = $state(0);</script>
<div class="flex size-full flex-col gap-2"> <label class="flex items-center gap-2 text-sm text-default"> Heading <input type="range" min="0" max="359" bind:value={heading} /> {heading}° </label> <div class="grow border border-light"> <MapLibre center={new LngLat(-73.9845, 40.7718)} zoom={17}> <MapLibreMarker lng={-73.985} lat={40.772} color="blue" /> <DirectionalMarker lng={-73.984} lat={40.7716} rotation={heading} /> </MapLibre> </div></div>Coordinates
Section titled “Coordinates”LngLatInput needs no map. It calls oninput with a new LngLat when either field takes a number, and never for an empty field or a latitude outside -90 to 90. The latitude keys and scrub stop at those bounds.
40.7718116, -73.984421
Code
<script lang="ts"> import type { LngLat } from "maplibre-gl";
import { LngLatInput } from "@viamrobotics/prime-map";
let lng = $state(-73.984_421); let lat = $state(40.771_811_6);
const update = (next: LngLat) => { lng = next.lng; lat = next.lat; };</script>
<div class="flex w-80 flex-col gap-2"> <LngLatInput label="Waypoint" {lng} {lat} oninput={update} /> <p class="text-sm text-default">{lat}, {lng}</p></div>Three.js
Section titled “Three.js”useMapLibreThreeRenderer(scene, camera, render) adds a custom layer that copies the map’s projection onto camera.current and places every object whose userData.lngLat holds a { lng, lat }. A THREE.Line takes a list of points. useMapLibreThreeRaycast(camera) returns a pointer and a compute(raycaster) for picking those objects. Import both from @viamrobotics/prime-map/three, and call them in a component inside a MapLibre.
MapLibre
Section titled “MapLibre”| Prop | Type | Default | Description |
|---|---|---|---|
minPitch | number | 0 | The minimum camera pitch. |
maxPitch | number | 60 | The maximum camera pitch. |
bind:zoom | number | 9 | The camera zoom. The camera writes it back as the map zooms. |
minZoom | number | 0 | The minimum zoom level of the map (0-24). |
maxZoom | number | 21 | The maximum zoom level of the map (0-24). |
class | ClassValue | Classes for the map's outer box. | |
bind:center | LngLat | the Viam office, lng -73.984421, lat 40.7718116 | The camera center. The camera writes it back as the map moves. |
bind:map | Map | The maplibre-gl Map instance, bound once the map is created. | |
options | Partial<MapOptions> | Options passed to the maplibre-gl Map constructor, over the ones from these props. | |
mapProvider | MapProvider | 'open-street' | The tile provider. |
mapProviderKey | string | The API key for the tile provider. Google Maps requires one. | |
bind:satellite | boolean | false | Whether satellite imagery shows. |
children | Snippet | Content rendered inside the map once it loads. | |
layer | Snippet | Content rendered after children once the map loads. |
Plus everything in Omit<HTMLAttributes<HTMLDivElement>, 'children'>, spread onto the element.
MapLibreMarker
Section titled “MapLibreMarker”| Prop | Type | Default | Description |
|---|---|---|---|
lng | number | 0 | The longitude of the marker. |
lat | number | 0 | The latitude of the marker. |
rotation | number | 0 | The rotation of the marker, clockwise in degrees. |
scale | number | 1 | The relative size of the marker. |
color | string | '' | The color of the default pin. |
element | HTMLElement | An element drawn in place of the default pin. |
DirectionalMarker
Section titled “DirectionalMarker”| Prop | Type | Default | Description |
|---|---|---|---|
lng | number | 0 | The longitude of the marker. |
lat | number | 0 | The latitude of the marker. |
rotation | number | 0 | The heading the arrow points along, clockwise in degrees. |
scale | number | 1 | The relative size of the marker. |
LngLatInput
Section titled “LngLatInput”| Prop | Type | Default | Description |
|---|---|---|---|
label | string | 'Latitude, longitude' | The accessible name of the pair. |
readonly | boolean | false | Whether both fields are read-only. |
aria-describedby | string | The id of the element that describes both fields. | |
lng | number | The longitude value. | |
lat | number | The latitude value. | |
children | Snippet | Content after the two fields, such as a button. | |
oninput | (lngLat: LngLat) => void | Called with the new pair when the longitude takes a finite number or the latitude takes a finite number from -90 to 90. |
FollowControls
Section titled “FollowControls”| Prop | Type | Default | Description |
|---|---|---|---|
lng | number | The longitude of the point to follow. | |
lat | number | The latitude of the point to follow. | |
bind:following | boolean | false | Whether the map follows the point. |
NavigationControls
Section titled “NavigationControls”| Prop | Type | Default | Description |
|---|---|---|---|
position | ControlPosition | 'top-right' | The map corner the controls sit in. |
showCompass | boolean | true | Whether the compass shows. |
showZoom | boolean | true | Whether the zoom buttons show. |
visualizePitch | boolean | true | Whether the compass tilts with the camera pitch. |
CenterControls
Section titled “CenterControls”Takes describedby }: Props as its props.