HTML Geolocation API


Lesson 54 of 58

The Geolocation API lets you get the user’s current location. This is how websites show nearby restaurants, weather in your area, or directions from your current position.

Privacy Note: The browser will ALWAYS ask for permission before sharing location. Your privacy is protected. The location is only available if the user clicks “Allow”.
Tip: The Geolocation API works best on devices with GPS, like smartphones and smartwatches. On desktop computers, it uses Wi-Fi and IP address, which is less accurate. It also requires a secure connection (HTTPS).

Browser Support

The Geolocation API is supported in all modern browsers:

Feature Chrome Edge Firefox Safari Opera
Geolocation API 5.0 12.0 3.5 5.0 10.6

Using the Geolocation API

The Geolocation API is accessed through navigator.geolocation. Here is a simple example that gets your current latitude and longitude:

Get Current Location

When you click the button, the browser will ask for permission. If you allow it, your latitude and longitude will appear.


How It Works – Step by Step

  1. Check if Geolocation is supported – The code checks if navigator.geolocation exists.
  2. Ask for permission – The browser automatically shows a permission request.
  3. Get the position – If allowed, getCurrentPosition() gets the location.
  4. Success or error – If successful, the position is displayed. If an error occurs, an error message is shown.

Error Handling (Specific Errors)

Sometimes getting the location fails. The user might deny permission, the GPS signal might be weak, or the request might time out. You can handle different errors with specific messages:

Geolocation with Error Handling

This example tells you exactly why the location could not be retrieved:

  • Permission denied – The user clicked “Block”
  • Position unavailable – GPS signal is weak or not available
  • Timeout – The request took too long
  • Unknown error – Something else went wrong

What Information Can You Get?

The getCurrentPosition() method returns an object with several properties. Some are always available, others depend on the device:

Property Description Always Available?
coords.latitudeThe latitude as a decimal number✅ Yes
coords.longitudeThe longitude as a decimal number✅ Yes
coords.accuracyAccuracy of the position (in meters)✅ Yes
coords.altitudeAltitude in meters above sea level📱 GPS devices only
coords.altitudeAccuracyAltitude accuracy (in meters)📱 GPS devices only
coords.headingDirection (degrees from North)📱 GPS devices only
coords.speedSpeed in meters per second📱 GPS devices only
timestampDate and time of the response✅ Yes

Real-Time Location Tracking (watchPosition)

The watchPosition() method continuously tracks the user’s location as they move. This is useful for GPS navigation or fitness tracking apps.

Watch Position (Real-Time Tracking)

The watchPosition() method keeps updating the location. Use clearWatch() to stop tracking when you no longer need it.


Real-World Uses for Geolocation

  • Weather websites – Show local weather automatically
  • Restaurant finders – Find places to eat near you
  • Maps and navigation – Show directions from your current location
  • Delivery tracking – Track food or package delivery in real time
  • Social media check-ins – Share your location on ModafVibe, Facebook, or Instagram
  • Fitness apps – Track running or cycling routes

Quick Summary

  • Use navigator.geolocation to access the Geolocation API.
  • The browser always asks for permission first.
  • getCurrentPosition() gets the location once.
  • watchPosition() continuously tracks location as it changes.
  • clearWatch() stops the tracking.
  • Always handle errors (permission denied, timeout, position unavailable).
  • Latitude and longitude are always returned. Accuracy, altitude, speed, and heading depend on the device.

What Just Happened? Let Me Explain

  • Geolocation requires user permission for privacy. You cannot get someone’s location without asking.
  • The API works best on smartphones with GPS. Desktop computers use Wi-Fi and IP address, which are less accurate.
  • Always include error handling. Users might deny permission, or the GPS signal might be weak.
  • If you only need the location once, use getCurrentPosition(). If you need continuous updates (like a navigation app), use watchPosition().
  • Remember to call clearWatch() when you no longer need tracking to save battery.
Tip: Test your geolocation code on a smartphone if possible. Desktop browsers often show less accurate positions. Some browsers also require HTTPS for the Geolocation API to work.

Geolocation API Reference

Method / Property Description
navigator.geolocationThe main geolocation object
getCurrentPosition(success, error, options)Gets the current location once
watchPosition(success, error, options)Continuously tracks location
clearWatch(watchId)Stops the watchPosition tracking
coords.latitudeLatitude in decimal degrees
coords.longitudeLongitude in decimal degrees
coords.accuracyAccuracy in meters
coords.altitudeAltitude in meters (GPS only)
coords.speedSpeed in meters per second (GPS only)
coords.headingDirection in degrees (GPS only)
timestampTime when the position was retrieved

Lesson 54 of 58