Get Longitude And Latitude Of Current Location

Step 1

Write the HTML aspect.

<!DOCTYPE html>
<html>
<body>
    <p>Click the button to get your coordinates.</p>
    <button onclick="getLocation()">Click now</button>
    <p id="demo"></p>
   </body>
</html>

Step 2

Write the Javascript code to be inserted into the HTML body section.

<script>
var 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>

Finally, let us see the whole code(i.e both HTML and Javascript)

<!DOCTYPE html>
<html>
<body>
    <p>Click the button to get your coordinates.</p>
    <button onclick="getLocation()">Click now</button>
    <p id="demo"></p>
    <script>
var 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>
</body>
</html>

Once the button is clicked, the Longitude and latitude coordinates of the user's current location will be displayed.

Get Longitude and Latitude of Current location