logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

HTML Geolocation API

The HTML Geolocation API allows users to share their location with web applications. This is widely used for mapping, finding nearby points of interest, and navigation.


Important Privacy Note

To protect user privacy, the location is never available without the user's explicit approval. Additionally, this API is only available on secure contexts (HTTPS).


How to Use the Geolocation API

The API is accessed through the navigator.geolocation object. The most common method is getCurrentPosition(), which retrieves the user's latitude and longitude.


Basic Example:

HTML

<button onclick="getLocation()"> Get Coordinates </button>

<p id="demo"> </p>

<script>

const x = document.getElementById("demo");

function getLocation() {

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(success, error);

} else {

x.innerHTML = "Geolocation is not supported by this browser.";

}

}


function success(position) {

x.innerHTML = "Latitude: " + position.coords.latitude +

"<b>Longitude: " + position.coords.longitude;

}


function error() {

x.innerHTML = "Unable to retrieve your lo

}

</script>


Advanced Error Handling

The getCurrentPosition() method accepts a second function to handle errors. Using a switch statement allows you to provide specific feedback based on the failure reason.

Javascript

<script>

function error(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;

case error.UNKNOWN_ERROR:

x.innerHTML = "An unknown error occurred.";

break;

}

}

</script>


The Data Object

On success, the API returns a Coordinates object. Some properties are always available, while others depend on the device's hardware (like GPS).


Property Description
coords.latitude The latitude as a decimal (Always).
coords.longitude The longitude as a decimal (Always).
coords.accuracy The accuracy level of the position (Always).
coords.altitude Altitude in meters above sea level (If available).
coords.speed The speed in meters per second (If available).
timestamp The date/time of the response.

Real-Time Tracking: watchPosition()

For apps that require movement tracking (like turn-by-turn navigation), use watchPosition(). It updates the location automatically as the user moves.

  • watchPosition():Returns the current position and continues to return updated data.

  • clearWatch():Stops the watchPosition() method.


Tracking Example:

Javascript

<script>

var watchID = navigator.geolocation.watchPosition(success, error);


// To stop tracking later:

// navigator.geolocation.clearWatch(watchID);

</script>


Browser Support

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

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis