A speed number is useful. A speed number plus a line on a map showing where you've been is far more useful, and it's surprisingly little extra code. Here's how to add a live route, a follow mode that doesn't fight the user, and a GPX download that Strava and Google Earth can open.
The examples use Leaflet and OpenStreetMap tiles because they're free and small, but the ideas carry over to any map library.
1. Set up the map and the route line
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script> <div id="map" style="height: 70vh"></div> const map = L.map('map').setView([0, 0], 2); L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: '© OpenStreetMap contributors' }).addTo(map); const route = L.polyline([], { color: '#ff7a00', weight: 5 }).addTo(map); let marker = null; const track = []; // we keep our own copy for GPX export The public OpenStreetMap tile servers have a usage policy, so for anything with real traffic, switch to a tile provider you're allowed to use at scale.
2. Append each GPS fix
navigator.geolocation.watchPosition(onFix, console.error, { enableHighAccuracy: true, maximumAge: 0 }); function onFix(pos) { const { latitude: lat, longitude: lon, accuracy } = pos.coords; if (accuracy > 50) return; // skip weak fixes, they draw zig-zags const point = [lat, lon]; track.push({ lat, lon, time: new Date(pos.timestamp).toISOString() }); route.addLatLng(point); if (!marker) { marker = L.circleMarker(point, { radius: 8 }).addTo(map); map.setView(point, 16); } else { marker.setLatLng(point); } if (following) map.panTo(point); } Skipping fixes with poor accuracy matters more on a map than on a gauge: one bad fix turns a straight road into a spike.
3. A follow mode that respects the user
Nothing is more annoying than dragging the map to look around and having it snap back a second later. Stop following when the user drags, and give them a button to resume:
let following = true; map.on('dragstart', () => { following = false; }); document.querySelector('#locate').addEventListener('click', () => { following = true; if (marker) map.panTo(marker.getLatLng()); }); 4. Point the marker the way you're heading
coords.heading gives the direction of travel in degrees clockwise from north, but it's null when you're stationary and unreliable at walking pace. Only rotate the marker when speed is above a few km/h, and keep the last good heading otherwise.
5. Export the route as GPX
GPX is plain XML, so you don't need a library:
function toGPX(points, name = 'Trip') { const pts = points.map(p => `<trkpt lat="${p.lat}" lon="${p.lon}"><time>${p.time}</time></trkpt>` ).join('\n'); return `<?xml version="1.0" encoding="UTF-8"?> <gpx version="1.1" creator="my-speedometer" xmlns="http://www.topografix.com/GPX/1/1"> <trk><name>${name}</name><trkseg> ${pts} </trkseg></trk></gpx>`; } function downloadGPX() { const blob = new Blob([toGPX(track)], { type: 'application/gpx+xml' }); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'trip.gpx'; a.click(); URL.revokeObjectURL(a.href); } Strava, Google Earth and most mapping apps will import that file as-is.
6. Keep it on the device
Store finished trips in IndexedDB or localStorage rather than on a server. A route is a precise record of where someone went, and users trust a tool that never uploads it.
See it working
If you'd like to see all of this in a finished product, try this free GPS speedometer with map. It draws your route live, stops following when you drag, and saves trips on your phone with a GPX download.
![[Herdr in Practice] Build Your Own Herdr Mod: Create a "Pending Review" Queue with Labels to Instantly Track Unread Reports](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flasxwu5u1is3pu1fn6gi.png)