Utilities
Geometry helpers for distance, length, area, containment and building shapes.
<script lang="ts">
import {
Map,
MapMarker,
MapPolygon,
MapPolyline,
circleRing,
distanceMeters,
pointInPolygon,
type LngLatTuple
} from '$lib/components/ui/map/index.js';
/** Free delivery within this many metres of the shop. */
const RADIUS = 1500;
let shop = $state<LngLatTuple>([-0.1246, 51.5007]);
let home = $state<LngLatTuple>([-0.1419, 51.5014]);
const zone = $derived(circleRing(shop, RADIUS));
const distance = $derived(distanceMeters(shop, home));
const inside = $derived(pointInPolygon(home, [zone]));
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [-0.13, 51.502], zoom: 13, bearing: 0, pitch: 0 }}>
<MapPolygon positions={zone} fillOpacity={0.1} />
<MapPolyline positions={[shop, home]} dashArray={[2, 2]} />
<MapMarker bind:lngLat={shop} draggable aria-label="Shop" />
<MapMarker bind:lngLat={home} draggable aria-label="Customer" />
</Map>
<p
class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
aria-live="polite"
>
{Math.round(distance)} m away, {inside ? 'free delivery' : 'outside the free zone'}
</p>
</div>
The map item exports a few plain functions for the maths you need around a map. They run anywhere, including on the server, and need no map instance. Distances use a spherical earth with a mean radius of 6,371,008.8 m, which is close enough for anything at city or country scale.
Import them from the map barrel:
import { distanceMeters, circleRing, pointInPolygon } from '$lib/components/ui/map/index.js';Types
type LngLatTuple = [number, number]; // [longitude, latitude]
type LngLatBoundsTuple = [LngLatTuple, LngLatTuple]; // [[west, south], [east, north]]Positions are always longitude first, the same order GeoJSON uses. A ring is a LngLatTuple[] that outlines a polygon.
Measuring
distanceMeters
distanceMeters(from: LngLatTuple, to: LngLatTuple): numberReturns the great-circle distance between two positions, in metres.
distanceMeters([-0.1276, 51.5072], [2.3522, 48.8566]); // about 343,500lineLength
lineLength(line: LngLatTuple[]): numberReturns the length of a line along the ground, in metres. A line with fewer than two positions has length 0.
const km = lineLength(route) / 1000;ringArea
ringArea(ring: LngLatTuple[]): numberReturns the area a ring encloses, in square metres. The ring can be open or closed and wind either way. It is accurate for city- and field-sized shapes. Fewer than three corners give 0.
const hectares = ringArea(field) / 10_000;polygonArea
polygonArea(rings: LngLatTuple[][]): numberReturns the area of a polygon in square metres: the first ring minus every ring after it, which are holes. It never goes below 0. The rings are in the same shape as GeoJSON Polygon coordinates.
const area = polygonArea(feature.geometry.coordinates as LngLatTuple[][]);Containment
pointInPolygon
pointInPolygon(point: LngLatTuple, rings: LngLatTuple[][]): booleanReturns true when the point is inside the first ring and outside every hole. Rings may cross the antimeridian, as long as each is less than 180° wide. A point exactly on an edge may go either way.
const inZone = pointInPolygon(customer, [zoneRing]);ringCenter
ringCenter(ring: LngLatTuple[]): LngLatTupleReturns the average of a ring’s corners. It is a good label spot for convex shapes; for an L or a C shape it can fall outside. Throws on an empty ring.
const labelAt = ringCenter(zoneRing);Building shapes
circleRing
circleRing(center: LngLatTuple, radius: number, steps = 64): LngLatTuple[]Returns a closed ring that traces a circle of radius metres on the ground, with steps + 1 positions. Throws when radius is negative or steps is not a whole number of at least 3.
<MapPolygon positions={circleRing([-0.1246, 51.5007], 1500)} />rectangleRing
rectangleRing(bounds: LngLatBoundsTuple): LngLatTuple[]Returns a closed ring of five positions around the bounds, starting at the south-west corner.
rectangleRing([
[13.37, 52.5],
[13.43, 52.53]
]);closeRing
closeRing(ring: LngLatTuple[]): LngLatTuple[]Returns a copy of the ring with its first position repeated at the end, as GeoJSON polygons need. A ring that is already closed, or empty, comes back as a copy unchanged.
const polygon = { type: 'Polygon', coordinates: [closeRing(corners)] };Lines
splitLine
splitLine(line: LngLatTuple[], progress: number): SplitLine
interface SplitLine {
done: LngLatTuple[]; // from the start to point
rest: LngLatTuple[]; // from point to the end
point: LngLatTuple; // where the line was split
}Splits a line at progress, from 0 to 1, of its length on the ground. progress is clamped to that range. Throws on an empty line. Route uses it to draw progress.
const { point } = splitLine(route, 0.4); // where the courier is nowlineHeadingAt
lineHeadingAt(line: LngLatTuple[], progress: number): numberWhich way the line runs where splitLine splits it, in degrees clockwise from north. It follows the line as the map draws it, so an icon rotated by it lines up with the route. Repeated positions are skipped. Returns 0 for a line with no length.
<MapMarker
lngLat={splitLine(route, 0.4).point}
aria-label="Courier"
rotation={lineHeadingAt(route, 0.4)}
rotationAlignment="map"
/>arcLine
arcLine(from: LngLatTuple, to: LngLatTuple, curvature = 0.2, steps = 64): LngLatTuple[]Returns a curved line with steps + 1 positions. At its middle the curve sits curvature times the straight distance to the side, measured in degrees. It crosses the antimeridian the short way, so the last longitude can pass ±180. Throws when steps is not a whole number of at least 1. Arc uses it.
<MapPolyline positions={arcLine([8.57, 50.03], [-9.14, 38.77])} />Scale
scaleBar
scaleBar(meters: number, unit: 'metric' | 'imperial' = 'metric'): ScaleBar | null
interface ScaleBar {
label: string; // such as '500 m' or '2 mi'
ratio: number; // share of the measured width the bar covers, 0 to 1
}Takes the ground length of a stretch of map, in metres. Returns the largest round length (1, 2, 3 or 5 times a power of ten) that fits, as a label, and the share of the stretch it covers. Metric uses m and km; imperial uses ft and mi. Returns null when meters is not a positive finite number. Scale Control is built on it.
scaleBar(740); // { label: '500 m', ratio: 0.676 }
scaleBar(740, 'imperial'); // { label: '2000 ft', ratio: 0.824 }