Services and field tools
Coordinates, MGRS and grids
Parse common coordinate formats and show consistent military and geographic grids.
On this page
Convert and parse positions #
Prerequisites: install the standalone dependency-free MGRS package or import the hosted module. It works in browsers, workers, Node and React Native without MapLibre.
npm install https://maps.example/npm/sw-maps-mgrs-1.0.0.tgzimport { toMgrs, parseMgrs, parsePosition, toDms, toDd,
mgrsCell, utmFromLngLat, lngLatFromUtm } from '@sw-maps/mgrs';
const at = [120.9842, 14.5995];
console.log(toMgrs(at)); // 51P TS 82848 14992 (1 m)
console.log(toMgrs(at, 1000)); // 51P TS 82 14 (1 km)
console.log(parseMgrs('51PTS8214').center);
console.log(parsePosition('14.5995, 120.9842').lngLat);
console.log(toDms(at), toDd(at));
const utm = utmFromLngLat(at);
console.log(lngLatFromUtm(utm));
const square = mgrsCell(at, 1000); // GeoJSON Polygon with cell properties
console.log(square);The human coordinate parser accepts common latitude-first input; its returned lngLat always uses longitude first. API query strings and GeoJSON remain longitude first. MGRS digits are truncated, not rounded. parseMgrs returns the square center and cell information; a coarse reference identifies an area, not an exact observed fix.
Display grids #
import { setGrid, gridState, mgrsGridStep } from 'https://maps.example/sw-map.js';
map.once('load', () => {
setGrid(map, { mgrs: true, graticule: true });
console.log(gridState(map), mgrsGridStep(map));
});
// Disable both: setGrid(map, { mgrs: false, graticule: false });Grid generation covers the visible area and is restored after a style change. MGRS shows zone lines, 100 km squares from zoom 5, 10 km from 8, 1 km from 11.5 and 100 m from 14.5. The graticule adapts from degrees to seconds. Grid precision describes displayed cells, not GPS accuracy.
Precision and edge cases #
| Value in metres | MGRS cell |
|---|---|
| 100000 | 100 km square |
| 10000 | 10 km |
| 1000 | 1 km |
| 100 | 100 m |
| 10 | 10 m |
| 1 | 1 m |
UTM/MGRS helpers cover latitudes −80° to +84° and account for Norway/Svalbard zone exceptions. They do not provide UPS polar references. Check for null parse results and handle errors instead of guessing a malformed reference. mgrsCell clips a square to its grid-zone boundary; it can be used to build an app’s own sector editor. The viewer’s selected sectors and export workflow are described in Use the reference viewer.