1. Introduction to navigator.geolocation
The HTML5 Geolocation API allows web applications to access the user’s physical latitude and longitude. Depending on the user’s hardware and device settings, location is determined using:
- GPS (Global Positioning System): High precision outdoors (smartphones, cellular devices).
- Wi-Fi Triangulation: Nearby Wi-Fi network BSSIDs and signal strength.
- IP Address Lookup: City/Country level coarse estimation when hardware GPS is unavailable.
- Cellular Tower Triangulation: Mobile network tower signals.
navigator.geolocation on unencrypted HTTP pages will fail silently or throw a security error (except on localhost for local development).
Checking Browser Geolocation Support
Click below to test if your current browser and origin support the HTML5 Geolocation API live:
2. Geolocation Permission & Resolution Lifecycle
User privacy is paramount. Browsers will never share user coordinates automatically. When a website requests location data for the first time, an explicit browser permission prompt is displayed to the user.
Geolocation API Request & Resolution Workflow
Querying Permissions API Programmatically
Query the browser's Permissions API to see whether location access is granted, prompt, or denied:
3. Retrieving Coordinates with getCurrentPosition()
The navigator.geolocation.getCurrentPosition() method requests a one-time location snapshot. It accepts three parameters:
| Position Property | Type | Description |
|---|---|---|
coords.latitude | Number (decimal) | Latitude in degrees (e.g. 37.7749). |
coords.longitude | Number (decimal) | Longitude in degrees (e.g. -122.4194). |
coords.accuracy | Number (meters) | The accuracy level of latitude & longitude in meters. |
coords.altitude | Number or null | Height above sea level in meters (if hardware supports). |
coords.speed | Number or null | Current velocity in meters per second. |
timestamp | Epoch MS | Timestamp when position snapshot was recorded. |
Configuring PositionOptions
Click to fetch your live coordinates and generate an OpenStreetMap location link:
4. Continuous Tracking with watchPosition() & clearWatch()
For navigation apps, fitness trackers, or live delivery maps, watchPosition() registers a handler that fires automatically whenever the device’s physical position changes.
Subscribing & Unsubscribing Movement Stream
Start live position watching to see continuous position updates:
5. Error Handling & PositionError Codes
When location retrieval fails, the error callback receives a PositionError object containing a numerical code and a descriptive message:
| Error Code | Constant Name | Cause & Recommended Recovery Action |
|---|---|---|
1 |
PERMISSION_DENIED |
User clicked "Block" on the prompt. Prompt user with manual location search bar. |
2 |
POSITION_UNAVAILABLE |
Location resolution failed (no GPS fix, offline). Fallback to IP address geolocation API. |
3 |
TIMEOUT |
Resolution exceeded options.timeout limit. Retry with higher timeout or enableHighAccuracy: false. |
Robust Error Handling Pattern
Select a simulated PositionError code to test the handler logic:
Hands-On Challenge: Build a Store Distance Finder
Write a function that uses the Haversine Formula to compute the distance in kilometers between the user’s live coordinates and a target store location (e.g. Latitude: 37.7749, Longitude: -122.4194).
Chapter 16 Key Takeaways
navigator.geolocationrequires an HTTPS secure context.- User permission is required before any location data is returned.
- Use
getCurrentPosition()for single snapshots; usewatchPosition()for continuous movement updates. - Always pass an error callback to handle
PERMISSION_DENIED,POSITION_UNAVAILABLE, andTIMEOUT. - Use
enableHighAccuracysparingly as hardware GPS consumes higher device battery power.