QA
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→

More HTML & Semantic Web Interview Questions

Beginner LevelHTML & Semantic Web

What is Semantic HTML and why is it essential for modern web applications?

Beginner LevelHTML & Semantic Web

What is <!DOCTYPE html> and what happens if it is omitted?

Beginner LevelHTML & Semantic Web

What is the difference between inline and block-level HTML elements?