Category Archives: JS

Geolocation API

The Geolocation API of web browsers allows websites to request access to the user’s geographical location. The API returns the user’s latitude and longitude coordinates (among other data).

Here is an example code snippet of how to use the Geolocation API:

if ("geolocation" in navigator) {
  // check if geolocation is supported by the browser
  navigator.geolocation.getCurrentPosition(position => {
    // if access is granted, do something with the data
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    alert(`Your location is (${latitude}, ${longitude})`);
  });
} else {
  alert("Geolocation is not supported by this browser.");
}

Continue reading Geolocation API