navigator.geolocation estimates position from GPS, Wi-Fi and IP data. getCurrentPosition(success, error, options) asks once; watchPosition() reports every move and returns an id for clearWatch(id). The first call prompts for permission, so make it from a click.
const where = () => new Promise((resolve, reject) =>
navigator.geolocation.getCurrentPosition(resolve, reject,
{ enableHighAccuracy: true, timeout: 5000, maximumAge: 60_000 }));
try {
const { coords } = await where();
const { latitude, longitude, accuracy, speed } = coords;
console.log(`${latitude}, ${longitude} ±${accuracy} m, speed ${speed}`);
} catch (err) {
console.log(`Error ${err.code}: ${err.message}`); // 1 denied, 2 unavailable, 3 timeout
}Output
5.9804, 116.0735 ±20 m, speed null
altitude, heading and speed stay null without GPS. For an address, call a geocoding service such as OpenStreetMap 579 's Nominatim 372,997 . Chrome 144 1 added a declarative <geolocation> element: a browser-drawn button that also lets users undo an earlier denial.