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