Map
Base maps and layers, routes, points, measuring and drawing. Built on MapLibre; imported from @kystdata/ui/map so pages without a map don't load it.
Playground
Hover the green track for speed and time. Layers, search and the ruler are on the map.
<Map accessToken={env.PUBLIC_MAPBOX_TOKEN} measureKey={mission.id} fitLabel="Zoom to waypoints">
<MapRoute points={track} hover />
<MapRoute points={plan} dashed color="var(--color-fg-subtle)" />
{#each waypoints as w (w.id)}
<MapMarker lnglat={w.at} status={w.status} label={w.name} />
{/each}
{#each stations as s (s.id)}
<MapMarker lnglat={s.at} shape="diamond" color="#7c3aed" label={s.name} />
{/each}
</Map> Base maps and layers
The Layers button, top of the controls. Mapbox is behind Simple (its light style, or our own dark one, following the theme) and Satellite; the rest is Kartverket's open map cache, no key.
- One base map at a time, chosen from thumbnails of the real thing. Simple is the default: quiet land and water, so the data on it stands out.
- Overlays sit on the base map and under the points, grouped by what they tell you. Soundings go on top, so the numbers stay readable.
- The choice is remembered per browser, under the same keys as the web app, so it follows the user from map to map.
- Raster maps (sea chart, topographic, satellite) keep their own colours in dark too: a chart is read, so it isn’t dimmed or tinted.
- A page with its own data overlays (water bodies, the project’s areas) hands them to the map, and they join the panel in their group.
Routes
- Solid is where the vessel went; dashed is where it will go. A planned route takes a quiet colour, so the travelled one leads.
- Where the log has a gap of more than an hour, the line breaks and the gap is drawn faint and dotted: the track is unknown there, not straight.
- Hover snaps to the nearest logged point: a dot glides along the line and a card gives the speed and time. Where routes overlap, the nearest one answers.
- A plain 3px line in the route’s colour. The hover readout is dark, like a tooltip: SOG and the time, as the vessel log writes them.
- Points as a list of segments (a mission’s legs) join each to the next with the same faint link: paused, not sailed.
- dots marks each logged position, from a zoom you pick so a long track stays a line when zoomed out; times writes HH:MM beside each from zoom 14.
- arrows sets chevrons along the line, the way it goes, for a plan whose order matters. flow runs dashes along it, for a leg under way now; it holds still for readers who ask for less motion.
- colorBy="speed" colours each stretch by its speed, red when slow to green at speed. Always pair it with MapColorKey, so the colours read.
- onhover and onclick hand the nearest point to the page: a timeline that follows the pointer, a click that seeks. label adds a line to the hover card, such as a planned leg’s length.
Vessels and popups
MapVessel for a vessel on the map, popups on points, and a menu where the map is clicked.
- A vessel is an arrow turned to its heading, in its own colour, and turns with the map. Without a heading yet, a dot in that colour.
- live pulses a ring for a position coming in now. Give updated and the pulse stops, and the vessel fades, once the position is older than staleAfter (5 minutes): a vessel gone quiet must not look live.
- ahead draws a dashed line to where it will be in that many minutes on its course at its speed: 6 is the board’s.
- A popup on a vessel or marker opens on click and closes on a click elsewhere or Escape. Keep it short: a name, a reading or two, a link.
- MapPopup pins a card or a menu to any place: what a click on the sea offers. Map onclick gives that place, and ignores clicks on markers, popups and controls, and clicks while measuring or drawing.
- Map frames bounds again whenever they change: pick a vessel in a list and hand its bounds, and the map follows.
Measure
- The ruler in the controls opens a bar at the bottom of the map: Distance or Area, the running total, Undo, Clear and Done. The total is in the bar, so it never covers what’s being measured.
- Click to add points. Double-click or Enter finishes; Backspace takes back the last point; Escape stops.
- Distance gives each corner the distance so far, and the total in km and nautical miles. Area gives the area in its middle, and the perimeter in the bar.
- Corners stay draggable after, and the dot between two adds a corner.
- Measurements are remembered per map with measureKey, a vessel or a mission, so they survive a reload.
Drawing
Points and areas drawn on the map, as the survey request does it. The pieces are MapSketch for the shape being drawn or edited, MapArea for areas at rest, and draggable markers for points.
- The tools are a rail down the left of the map, under search, with one-key shortcuts: V to select, P for a point, A for an area. The current tool is lit, and a line at the bottom says what a click will do.
- An area is drawn like a measurement: click the corners, close on the first, or double-click or Enter. Escape cancels.
- Click an area to edit it: drag a corner, or press the dot between two to add one. Select a corner and Delete removes it, down to three.
- Points are diamonds, so they read apart from the waypoints a page already shows. Drag to move one.
- What’s drawn is listed beside the map with its size, and removed from there. The list is the record; the map is where you shape it.
Markers
- Shape says what kind of point it is; colour says what state it is in. Waypoints are circles. A second kind on the same map (stations, buoys, drawn points) is a diamond, so the two read apart without a legend.
- Colour comes from the status, as on a Badge: done is green with a check, attention amber, failed red, scheduled blue. A group of points with no status can carry its own colour instead.
- The icon inside is the status, or a sequence number when the order is the point. Never a decorative icon.
- Use sm, a 12px dot with no icon, when there are too many points for icons to read, or for points that are context rather than the subject.
- Names are drawn by the map, not as elements, so the map can drop the ones that collide. Zoom in and they come back.
- The selected point, the one open in a sheet or popup, grows and gets a ring. One at a time.
Export image
The image button in the playground's controls. For a report, a slide or a quote: a framed area at print resolution, with the map's points and names on it.
- Turn it on with exportImage where a map is worth handing on: a project, a mission, a quote. It adds Export image above Full screen.
- It opens simple: a title, the base map, the page shape and the format. Advanced adds the theme, text size, layers and where each box goes; switching back keeps what was set.
- The dialog is a copy of the map under a frame. The frame stays put and its corners resize it; pan and zoom the map under it. The frame is what comes out, markers, scale bar, north and title as drawn.
- Shape and size for where it goes: A4 or A3 at 300 dpi for print, 16:9 for a slide. What the preview shows is what comes out; Text size makes the map’s names and the boxes larger or smaller together.
- Base map and overlays are the image’s own, starting from the map’s, on a light map unless it shows a raster one. Changing them doesn’t change the map.
- The credits are always printed in the corner, as the map licences ask. They follow the layers chosen.
- Markers are redrawn in the image, with their status glyph or number. A custom icon or children inside one is not: the image shows the plain shape.
- Give markers and routes a legend label and they are listed in the image’s legend, one entry per label: "Waypoint, sampled", "Track, 12 Nov".
- PNG for a slide or a message, JPEG where size matters, PDF to print: a true A4 or A3 page with the map filling it, at 300 dpi.
- Two themes. Technical is a report map: ruled boxes, an alternating scale bar, the half-filled north arrow and a neatline, Arial in black on white. Minimal is for a slide or a page: rounded cards with a soft shadow, a plain scale line, the page’s font, no border.
- Title, legend and scale each go in any corner, picked with a PositionPicker, stacking when they share one. The title can also run as a band across the top. Leave the legend’s heading empty for none.
Sailing distance
Measure, By sea, in the playground. How far a vessel sails, not as the crow flies.
- Pass router and Measure gets By sea, between Distance and Area: click along the way, and each leg is routed by sea and drawn as the route, with the clicks as a faint guide under it.
- The total is in nautical miles, km beside it; each corner gets the distance so far.
- An end on land (a quay) is moved to the nearest water and the hop is drawn dashed and counted straight, as the routing service answers.
- A leg with no route is drawn dashed and counted straight, and the bar says why: a point on land, or no route found. Never a silent straight line.
- The map only draws and adds up. The routing needs a key, so it runs behind the app’s own proxy; this page uses a stand-in that bends each leg.
<script lang="ts">
import { Map, type SeaRouter } from '@kystdata/ui/map';
import { measureSailingDistanceCached } from '$ui/map/sailing-distance';
/* The app's routing client, through its /api/routing proxy. */
const router: SeaRouter = async ([fromLon, fromLat], [toLon, toLat]) => {
const leg = await measureSailingDistanceCached(
{ latitude: fromLat, longitude: fromLon },
{ latitude: toLat, longitude: toLon }
);
return leg.routed ? { ...leg, routed: true } : leg;
};
</script>
<Map {router} /> Controls
- Search for a place top left: fjords, islands and towns from Kartverket, water first.
- The controls are one column top right, in groups: Layers; zoom in, zoom out and Zoom to fit; Measure; then Full screen, last, as the one that changes the page rather than the map.
- Zoom to fit frames everything on the map. Name it for what that is on the page with fitLabel: Zoom to waypoints, Zoom to drawing.
- North shows only while the map is rotated or tilted, its needle following the bearing. Click it to put north back up.
- Each group is one card with a 1px inset, its buttons 1px less round, so a hover fills the card’s curve. Every control has a tooltip, opening to the left.
- Scale bottom left; attribution bottom right, collapsed to an i.