This lesson explains the Geolocation API from the very beginning and then moves into practical coding, useful options, common errors, privacy concerns, and interesting real-world applications. It also includes direct links to the most relevant MDN documentation pages.
The Geolocation API is a browser feature that allows a web page to ask for the user's geographical position. In simple words, it helps a website know where the user is, usually in the form of latitude, longitude, accuracy, and sometimes speed, heading, altitude, and a timestamp.
The main overview page on MDN is: Geolocation API.
This API is commonly used in map-based websites, route finders, delivery systems, travel tools, attendance systems, location sharing pages, emergency tools, and nearby search applications.
When a site wants your location, it asks the browser. The browser then checks whether the user allows it. If permission is granted, the browser tries to determine the position using one or more of these sources:
Very useful on phones and tablets. Often gives accurate outdoor location.
Nearby Wi-Fi information can help estimate the user's location.
Useful when GPS is weak or unavailable.
Usually less accurate, but may provide a rough area or city.
MDN also documents the entry point for the feature as Navigator.geolocation.
getCurrentPosition()This method gets the user's current position once. It is useful when you just need one location reading.
MDN reference: Geolocation.getCurrentPosition()
watchPosition()This method keeps watching the user's position and calls your success function again whenever the location changes. It is useful for live tracking.
MDN reference: Geolocation.watchPosition()
clearWatch()
This method stops a live watch created by watchPosition().
MDN reference: Geolocation interface
When geolocation succeeds, your callback receives a position object.
The most important values are inside position.coords.
MDN references: GeolocationPosition, timestamp, longitude, accuracy
| Property | Meaning |
|---|---|
position.coords.latitude |
The north-south position on Earth. |
position.coords.longitude |
The east-west position on Earth. |
position.coords.accuracy |
Estimated accuracy in meters. |
position.coords.altitude |
Height above sea level, if available. |
position.coords.heading |
Direction of travel, if available. |
position.coords.speed |
Speed in meters per second, if available. |
position.timestamp |
When the reading was taken. |
Both getCurrentPosition() and watchPosition() can accept an options object.
MDN documents these option fields on the method pages for
getCurrentPosition()
and
watchPosition().
| Option | Meaning |
|---|---|
enableHighAccuracy |
If true, the browser tries to get more accurate results. This may use more battery and may be slower. |
timeout |
Maximum time in milliseconds to wait for a reading. |
maximumAge |
How old a cached location can be before the browser must fetch a fresh one. |
enableHighAccuracy: true when accuracy matters a lot, such as route tracking or attendance by location.
Use lower accuracy or cached values when saving battery is more important.
Location requests can fail. So a good application must always handle errors.
MDN references: GeolocationPositionError, error.code, error.message
| Error code | Meaning |
|---|---|
1 / PERMISSION_DENIED |
The user blocked location access. |
2 / POSITION_UNAVAILABLE |
The browser could not determine the position. |
3 / TIMEOUT |
The location request took too long. |
Location is sensitive information. A responsible developer must handle it carefully.
For permission-related background, MDN also has: Using the Permissions API.
This is the classic beginner example. It checks if geolocation exists, then asks for one position reading.
This is useful when the user's location keeps changing, such as during walking, driving, or route recording.
Once you have latitude and longitude, you can build a map link.
This combines the Geolocation API with a map platform. The API provides coordinates, and the map platform shows the place visually.
A user presses a button and shares a map link with a friend, family member, or colleague.
Find nearby schools, hospitals, hotels, temples, shops, or bus stands.
Save movement points over time and draw the journey on a map.
Combine current location with mapping and routing services to suggest routes.
Track drivers, delivery people, or field workers in real time.
Mark attendance only if the user is physically present in a defined place.
Record the places a traveler visits and create a journey timeline.
Send the current position to emergency contacts quickly.
Use current coordinates to fetch local weather information.
Show nearby attractions, pilgrimage stops, museums, or heritage points.
Measure walking routes, running paths, and total distance traveled.
Build area-based notifications, check-ins, and personalized local dashboards.
These are the most useful MDN pages to study while learning or building with geolocation.
Main overview page for the API.
Hands-on guide with examples and error handling patterns.
The browser entry point that gives access to geolocation features.
Interface containing getCurrentPosition(), watchPosition(), and clearWatch().
One-time location read.
Continuous location updates for live tracking.
The success object returned to your callback.
Error object used when location retrieval fails.
Explains the numeric error codes.
Human-readable error description.
Reference for longitude inside the coordinates object.
Reference for the accuracy value in meters.
Reference for when the position reading was acquired.
Helpful if you want to understand permission flows better.
This demo lets you request your current position once, or start live tracking. It also shows the most important values returned by the browser.
The Geolocation API is one of the most practical browser APIs because it connects the digital world to the user's real-world position. With just a few lines of code, a web page can:
The most important things to remember are: