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

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *