Intermediate LevelHTML & Semantic Web
How does the Geolocation API retrieve device location with user consent?
❓ Interview Prompt: Explain navigator.geolocation.getCurrentPosition(), position options (enableHighAccuracy, timeout), and permission prompts.
The Geolocation API allows web applications to access the user's physical geographic location if the user grants explicit permission.
Usage:
Call 'navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options)'.
Security Rule: Geolocation is a Restricted Feature available ONLY on secure HTTPS origins!
Code Implementation Example
javascript
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log('Lat: ${position.coords.latitude}, Lng: ${position.coords.longitude}');
},
(error) => console.error('Geolocation error:', error.message),
{ enableHighAccuracy: true, timeout: 5000 }
);
}💡Retrieving user coordinates using Geolocation API with position options.
🎯 Interview Key Takeaway
Call navigator.geolocation.getCurrentPosition() on HTTPS origins to access user location upon permission approval.
#Geolocation#Permissions#Browser API#Intermediate
Back to all HTML & Semantic Web Questions→