📍 Web Geolocation API Complete Guide

1. Basic Geolocation with getCurrentPosition()

Click "Get Current Location" to start

2. getCurrentPosition() Method Properties

Property Type Description Example Value
coords.latitude Number Latitude in decimal degrees 40.7128
coords.longitude Number Longitude in decimal degrees -74.0060
coords.accuracy Number Accuracy of position in meters 25
coords.altitude Number Height in meters above WGS84 ellipsoid 50
coords.altitudeAccuracy Number Altitude accuracy in meters 10
coords.heading Number Direction of travel in degrees (0-360) 90
coords.speed Number Speed in meters per second 5.5
timestamp DOMTimeStamp Time when position was acquired 1640995200000

PositionOptions Parameters

Parameter Type Default Description
enableHighAccuracy Boolean false Request better accuracy (uses more battery)
timeout Number Infinity Maximum time (ms) to wait for position
maximumAge Number 0 Accept cached position younger than (ms)

3. Handling Errors and Rejections

Error Codes:

1 - PERMISSION_DENIED
User denied location permission

2 - POSITION_UNAVAILABLE
Location information unavailable

3 - TIMEOUT
Location request timed out

Error Properties:

code: Error code (1-3)

message: Human-readable error message

PERMISSION_DENIED: Constant (1)

POSITION_UNAVAILABLE: Constant (2)

TIMEOUT: Constant (3)

4. Displaying Location on a Map

Get your location to display on map

5. Location-specific Information

Get location to fetch local information

6. Complete Geolocation Application

Current Position

Not available

Accuracy

Not available

Last Updated

Never

Distance Traveled

0 meters