bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/HTML/Graphics, Media, and Browser Features
HTML•Graphics, Media, and Browser Features

HTML Geolocation API

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind HTML Geolocation API?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ x = document.getElementById("demo");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Using HTML Geolocation API
Locate the User's Position
HTML Geolocation API

The Geolocation API is used to get the user's current location.

Locate the User's Position

The Geolocation API is used to access the user's current location.

Since this can compromise privacy, the location is not available unless the user approves it.

Try It

Note

The Geolocation API is only available on secure contexts such as HTTPS.

Tip

The Geolocation API is most accurate for devices with GPS, like smartphones or smartwatches.

Browser Support

The numbers in the table specify the first browser version that fully supports Geolocation.

API
Geolocation5.012.03.55.010.6

Using HTML Geolocation API

The Geolocation API is accessed via a call to navigator.geolocation . This will cause the browser to ask the user for permission to access their location data. If the user accept, the browser will search for the best available functionality on the device to access this information (for example GPS).

The getCurrentPosition() method is used to return the user's current location.

The example below returns the latitude and longitude of the user's current location:

Example

Formatted code
<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 +
  "<br>Longitude: " + position.coords.longitude;

 }
function error() {
  alert("Sorry, no position available.");

  }
</script>

Live preview

Example explained

  • Check if Geolocation is supported
  • If Geolocation is supported, run the getCurrentPosition() method. If not, display a message to the user
  • The success() function outputs the user's location in latitude and longitude
  • The error() function alerts a text if the browser retrieves an error in getCurrentPosition()

Error Handling and Rejections

The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location.

Here is an example of a more specific error handling:

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;
 }
}

Location-specific Information

Geolocation is also very useful for location-specific information, like:

  • Up-to-date local information
  • Showing Points-of-interest near the user
  • Turn-by-turn navigation (GPS)

The getCurrentPosition() Method - Return Data

The getCurrentPosition() method returns an object on success. The latitude, longitude and accuracy properties are always returned. The other properties are returned if available:

PropertyReturns
coords.latitudeThe latitude as a decimal number (always returned)
coords.longitudeThe longitude as a decimal number (always returned)
coords.accuracyThe accuracy of position (always returned)
coords.altitudeThe altitude in meters above the mean sea level (returned if available)
coords.altitudeAccuracyThe altitude accuracy of position (returned if available)
coords.headingThe heading as degrees clockwise from North (returned if available)
coords.speedThe speed in meters per second (returned if available)
timestampThe date/time of the response (returned if available)

Geolocation Object - Other interesting Methods

The Geolocation object also has other interesting methods:

  • watchPosition() - Returns the current location of the user and continues to return updated location as the user moves (like the GPS in a car).
  • clearWatch() - Stops the watchPosition() method.

The example below shows the watchPosition() method. You need an accurate GPS device to test this (like a smartphone):

Example

Formatted code
 <script>
const x = document.getElementById("demo");

function getLocation() {

    if (navigator.geolocation) {
    navigator.geolocation.watchPosition(success,
  error);
  } else {

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

    }
}

 function success(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;

 }
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>

Live preview

Previous

HTML Audio

Next

HTML Plug-ins