HTML5 Geolocation API

 HTML5 Geolocation API


The Geolocation API allows you to retrieve the user's current geographical location using JavaScript. The user's permission is required to access this information. Here's an example:


<button onclick="getLocation()">Get Location</button>


<script>

  function getLocation() {

    if (navigator.geolocation) {

      navigator.geolocation.getCurrentPosition(showPosition);

    } else {

      alert("Geolocation is not supported by this browser.");

    }

  }


  function showPosition(position) {

    const latitude = position.coords.latitude;

    const longitude = position.coords.longitude;

    alert(`Latitude: ${latitude}, Longitude: ${longitude}`);

  }

</script>

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...