Detecting User's Geolocation in JavaScript

In this tip, we will learn how to detect a user's position using JavaScript's Geolocation API.

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(
    function(position) {
      console.log(position);
    },
    function(error) {
      console.error("Error Code = " + error.code + " - " + error.message);
    }
  );
} else {
  console.error("Geolocation is not supported by this browser.");
}

This code checks if the user's browser supports geolocation. If it does, it retrieves their current position and logs it to the console. If it doesn't, it logs an error message to the console. If an error occurs while retrieving the position (for example, the user denies permission), it logs the error code and message.

Copyright © 2024. Design and code by myself with Next.js. Fork it and create yours