JavaScript Geolocation API
Full Stack Web Development Courses with Real-time projects Start Now!!
Program 1
<html>
<body>
<center>
<h1>JavaScript Code to find location </h1>
<br>
<font color="blue" size="5">Click the button to show your coordinates.</font>
<br>
<button onclick="showLocation()">Click here to get location</button>
<h1 id="showresult"></h1>
<script>
const x = document.getElementById("showresult");
function showLocation()
{
try
{
navigator.geolocation.getCurrentPosition(showPosition);
}
catch(error)
{
x.innerHTML = error;
}
}
function showPosition(position)
{
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
</center>
</body>
</html>
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

