Skip to main content
HTML for Beginners
CHAPTER 18 Beginner

HTML Geolocation API

Updated: May 10, 2026
5 min read

# HTML Geolocation API

1. Introduction

The HTML Geolocation API allows a web application to request the user's physical location (latitude and longitude). This is used for "find stores near me" features, mapping applications, and local weather forecasts.

2. Learning Objectives

  • Understand privacy permissions.
  • Retrieve the user's current coordinates.
  • Handle location errors.

3. Detailed Explanations

Privacy is Paramount

A website cannot secretly track a user. When you request the location, the browser will ALWAYS popup a permission prompt: *"Website.com wants to know your location. Allow / Block"*.

Using navigator.geolocation

The JavaScript object navigator.geolocation handles all location tasks. The primary method is getCurrentPosition().
html
12345678910111213141516171819202122
<button onclick="getLocation()">Where am I?</button>
<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    // Check if the browser supports Geolocation
    if (navigator.geolocation) {
        // Request the position
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

// This function runs automatically once the user clicks "Allow"
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

Handling Errors

If the user clicks "Block" or their GPS is turned off, you need to handle the error gracefully.
javascript
123456789101112131415
navigator.geolocation.getCurrentPosition(showPosition, showError);

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
  }
}
This script grabs the user's location and generates a clickable link to Google Maps.
html
123456789101112131415
<button onclick="getMapLink()">Open My Location in Google Maps</button>
<div id="map-link"></div>

<script>
function getMapLink() {
    navigator.geolocation.getCurrentPosition(function(position) {
        let lat = position.coords.latitude;
        let lon = position.coords.longitude;
        let url = `https://www.google.com/maps?q=,`;
        
        document.getElementById("map-link").innerHTML = 
            `<a href="" target="_blank">Click to view on Map!</a>`;
    });
}
</script>

5. MCQs

Q1: True or False: The Geolocation API can retrieve a user's location without them knowing. A) True B) False *Answer: B (The browser always prompts for permission).*

6. Summary

The Geolocation API bridges the gap between the digital web and the physical world. By using navigator.geolocation.getCurrentPosition(), you can access powerful location data, provided the user trusts you enough to click "Allow".

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·