Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Elements

0/1

HTML Attributes

0/1

HTML Headings

0/1

HTML Paragraphs

0/1

HTML Styles

0/1

HTML Formatting

0/1

HTML Quotation

0/1

HTML Comments

0/1

HTML Colors

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Block and Inline

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML - The Head Element

0/1

HTML Style Guide

0/1

HTML Entities

0/1

HTML Symbols

0/1
Text lesson

HTML Geolocation

Locate the User’s Position

The HTML Geolocation API retrieves the geographical position of a user’s device, but this information is only accessible if the user grants permission due to privacy considerations.

Note that geolocation is most accurate on devices equipped with GPS, such as smartphones.

Browser Support

The numbers in the table indicate the earliest browser version that fully supports the Geolocation feature.

image 1

Note that starting from Chrome 50, the Geolocation API will only function on secure contexts like HTTPS. Requests to retrieve a user’s location will not work on non-secure origins such as HTTP.

Using HTML Geolocation

The getCurrentPosition() method retrieves the user’s position.

The example below demonstrates how to retrieve and display the latitude and longitude of the user’s position:

Example

<script>
const x = document.getElementById(“demo”);

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = “Geolocation is not supported by this browser.”;
  }
}

function showPosition(position) {
  x.innerHTML = “Latitude: “ + position.coords.latitude +
  “<br>Longitude: “ + position.coords.longitude;
}
</script>

Explanation of the example:

  1. Check if Geolocation is supported.
  2. If supported, call the getCurrentPosition() method; otherwise, display a message to the user.
  3. If getCurrentPosition() is successful, it returns a coordinates object to the specified function parameter (showPosition).
  4. The showPosition() function displays the Latitude and Longitude.

This example is a basic Geolocation script without error handling.

Handling Errors and Rejections

The second parameter of the getCurrentPosition() method is used to specify a function that runs if there is an error while attempting to retrieve the user’s location.

Example

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;
    case error.UNKNOWN_ERROR:
      x.innerHTML = “An unknown error occurred.”
      break;
  }
}

Location-specific Information

This page has demonstrated how to display a user’s position on a map.

Geolocation is also valuable for location-specific applications such as:

  • Providing up-to-date local information
  • Showing points of interest near the user
  • Offering turn-by-turn navigation (GPS)

The getCurrentPosition() Method – Return Data

The getCurrentPosition() method returns an object on success, which always includes the latitude, longitude, and accuracy properties. Additional properties are returned if available.

Property

Returns

coords.latitude

The latitude, always provided as a decimal number

coords.longitude

The longitude, always returned as a decimal number

coords.accuracy

The accuracy of the position, always provided

coords.altitude

The altitude above mean sea level in meters, returned if available

coords.altitudeAccuracy

The accuracy of the altitude of the position, returned if available

coords.heading

The heading in degrees clockwise from North, returned if available

coords.speed

The speed in meters per second, returned if available

timestamp

The date and time of the response, returned if available

Geolocation Object – Other interesting Methods

Additionally, the Geolocation object includes other useful methods:

  • watchPosition(): Retrieves the current position of the user and continuously updates it as the user moves, similar to a car’s GPS system.
  • clearWatch(): Halts the watchPosition() method.

The following example demonstrates the watchPosition() method. To test it, you’ll require a precise GPS device, such as a smartphone:

Example

<script>
const x = document.getElementById(“demo”);

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.watchPosition(showPosition);
  } else {
    x.innerHTML = “Geolocation is not supported by this browser.”;
  }
}
function showPosition(position) {
  x.innerHTML = “Latitude: “ + position.coords.latitude +
  “<br>Longitude: “ + position.coords.longitude;
}
</script>