Problem Statement
Which API allows web pages to access the user’s geographical location?
Explanation
The Geolocation API allows web applications to access the user’s location through the navigator.geolocation object. It can retrieve latitude, longitude, and accuracy. User permission is required for privacy reasons. Common uses include maps, location-based services, and delivery tracking.
Code Solution
SolutionRead Only
<!-- Basic Geolocation Example -->
<button onclick="getLocation()">Get Location</button>
<p id="output"></p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.getElementById('output').textContent = 'Geolocation not supported.';
}
}
function showPosition(position) {
document.getElementById('output').textContent =
`Latitude: ${position.coords.latitude}, Longitude: ${position.coords.longitude}`;
}
</script>