0
<!DOCTYPE html>
<html>
<body>
 <p>Click the button to get your coordinates.</p>
 <button onclick="getCoordinates()">Try It</button>
 <p id="workingcode"></p>
 <script>
var x = document.getElementById("workingcode");
 function getCoordinates() {
  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>

Output:

How to get current location coordinates in HTML
Working Code Asked question August 10, 2023