Services and field tools
Device GPS and NMEA receivers
Read live fixes locally, distinguish device capabilities, and manage receiver permissions and cleanup.
On this page
Use device location #
Prerequisites: HTTPS or localhost, browser geolocation support, user permission, and a location source. Phone/tablet GPS can work without internet; a desktop without GPS may depend on an unavailable online location provider.
import { createLocator, deviceSupported } from 'https://maps.example/sw-location.js';
if (!deviceSupported()) throw new Error('Secure device geolocation unavailable');
const locator = createLocator({
onFix(fix) { console.log(fix.lngLat, fix.accuracy_m, fix.time); },
onState(state) { console.log('Location state', state); },
onError(error) { console.error(error); }
});
// Call from your Locate button:
locator.startDevice();
// On Stop or component cleanup: locator.stop();Expected result: fixes delivered to onFix. Each fix may include lngLat, accuracy_m, altitude_m, speed_mps, heading_deg, satellites, hdop, source, time (epoch milliseconds), and estimated. Handle unavailable fields. The locator does not draw a dot or save a track; your app owns rendering, following, history retention and export.
Connect a USB/serial GPS receiver #
import { createLocator, serialSupported } from 'https://maps.example/sw-location.js';
const receiver = createLocator({ onFix: fix => console.log(fix),
onError: error => console.error(error) });
document.querySelector('#connect').addEventListener('click', async () => {
if (!serialSupported()) throw new Error('Web Serial unavailable');
await receiver.startSerial({ baudRate: 4800 });
});
document.querySelector('#stop').addEventListener('click', () => receiver.stop());Provide Connect and Stop buttons with those IDs. Use Chrome/Edge on a supported desktop with Web Serial. Choose the device in the permission picker and the baud rate required by the receiver, commonly 4800 or 9600. Do not assume a mobile browser has serial support.
The parser handles NMEA GGA, RMC and VTG from GPS and multi-GNSS talkers. Accuracy inferred from HDOP is estimated. A serial connection without a valid satellite fix does not establish a valid position.
Parse NMEA without a browser #
import { nmeaChecksumOk, parseNmea, nmeaAccumulator } from '@sw-maps/core/location';
const line = '$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47';
console.log(nmeaChecksumOk(line), parseNmea(line));
const acceptSentence = nmeaAccumulator(fix => console.log(fix));
acceptSentence(line);Pure NMEA helpers do not require navigator. Checksums are checked when present; sentences without checksums are accepted. A native application supplies its own USB/Bluetooth/serial transport and platform permission workflow.
Location troubleshooting and privacy #
- Permission denied: explain the purpose and let the user enable location in site/OS settings; do not repeatedly prompt.
- No fixes offline on a desktop: use a supported external receiver or GPS-equipped device.
- Position jumps or low accuracy: show the accuracy circle and timestamp, and avoid reporting stale fixes as live.
- Embedding: check geolocation Permissions-Policy and iframe allow permissions.
- Stop the locator when the user stops tracking or the owning view unmounts. Keep your app’s position-sharing and retention choices explicit.
SW location helpers make no network calls of their own. The browser/OS location provider may have its own behavior; verify the target device when disconnected.