Skip to content

@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.
  • MapLibreMarker and DirectionalMarker, a pin and a heading arrow.
  • CenterControls, FollowControls, NavigationControls, and SatelliteControls.
  • 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/three that renders a scene on the map.
  1. Install the package and its peers. The svelte peer is ^5.29.0 and the maplibre-gl peer is ^6.0.0. Add three only for the ./three subpath.

    Terminal window
    pnpm add @viamrobotics/prime-map @viamrobotics/prime-ui @viamrobotics/prime-controls @viamrobotics/tailwind-config maplibre-gl
  2. Import the stylesheets once, after Tailwind. MapLibre imports 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.

MapLibreMarker draws maplibre-gl’s pin, or an element you pass. DirectionalMarker draws an arrow that points along rotation, clockwise in degrees.

Code
Markers.svelte
<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>

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
Coordinates.svelte
<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>

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.

PropTypeDefaultDescription
minPitchnumber0The minimum camera pitch.
maxPitchnumber60The maximum camera pitch.
bind:zoomnumber9The camera zoom. The camera writes it back as the map zooms.
minZoomnumber0The minimum zoom level of the map (0-24).
maxZoomnumber21The maximum zoom level of the map (0-24).
classClassValueClasses for the map's outer box.
bind:centerLngLatthe Viam office, lng -73.984421, lat 40.7718116The camera center. The camera writes it back as the map moves.
bind:mapMapThe maplibre-gl Map instance, bound once the map is created.
optionsPartial<MapOptions>Options passed to the maplibre-gl Map constructor, over the ones from these props.
mapProviderMapProvider'open-street'The tile provider.
mapProviderKeystringThe API key for the tile provider. Google Maps requires one.
bind:satellitebooleanfalseWhether satellite imagery shows.
childrenSnippetContent rendered inside the map once it loads.
layerSnippetContent rendered after children once the map loads.

Plus everything in Omit<HTMLAttributes<HTMLDivElement>, 'children'>, spread onto the element.

PropTypeDefaultDescription
lngnumber0The longitude of the marker.
latnumber0The latitude of the marker.
rotationnumber0The rotation of the marker, clockwise in degrees.
scalenumber1The relative size of the marker.
colorstring''The color of the default pin.
elementHTMLElementAn element drawn in place of the default pin.
PropTypeDefaultDescription
lngnumber0The longitude of the marker.
latnumber0The latitude of the marker.
rotationnumber0The heading the arrow points along, clockwise in degrees.
scalenumber1The relative size of the marker.
PropTypeDefaultDescription
labelstring'Latitude, longitude'The accessible name of the pair.
readonlybooleanfalseWhether both fields are read-only.
aria-describedbystringThe id of the element that describes both fields.
lngnumberThe longitude value.
latnumberThe latitude value.
childrenSnippetContent after the two fields, such as a button.
oninput(lngLat: LngLat) => voidCalled with the new pair when the longitude takes a finite number or the latitude takes a finite number from -90 to 90.
PropTypeDefaultDescription
lngnumberThe longitude of the point to follow.
latnumberThe latitude of the point to follow.
bind:followingbooleanfalseWhether the map follows the point.
PropTypeDefaultDescription
positionControlPosition'top-right'The map corner the controls sit in.
showCompassbooleantrueWhether the compass shows.
showZoombooleantrueWhether the zoom buttons show.
visualizePitchbooleantrueWhether the compass tilts with the camera pitch.

Takes describedby }: Props as its props.