The Geolocation API exposes callbacks with latitude/longitude after explicit user permission.
Privacy expectations
- Explain why you request location—users deny opaque prompts.
- Prefer coarse accuracy when precision isn’t needed (
enableHighAccuracy). - Handle timeout and permanent denial states.
Alternatives
- Manual postal entry.
- IP-based coarse geolocation server-side (subject to law).
HTTPS requirement
Secure contexts protect sensitive APIs—test on localhost over HTTPS mirrors production.
Product realism
- Users deny location universally on the web—always supply manual workflows that don’t punish them.
- Accuracy bubbles on maps—communicate approximation to avoid liability in logistics.
Usage sketch (JavaScript)
const btn = document.querySelector('#geo-go');
btn?.addEventListener('click', () => {
navigator.geolocation.getCurrentPosition(
(pos) => console.log(pos.coords.latitude, pos.coords.longitude),
(err) => console.warn(err.code, err.message),
{ enableHighAccuracy: false, timeout: 8000 }
);
});
Triggered from a <button type="button"> after explaining why you need location—not on every page load.
Important interview questions and answers
- Q: What does progressive enhancement mean in API-driven pages?
A: Core tasks should work with baseline HTML first, then richer APIs enhance experience when supported. - Q: Why is feature detection better than browser sniffing?
A: It checks actual capability, avoids brittle UA assumptions, and degrades gracefully. - Q: What is the first accessibility check before shipping any page?
A: Verify keyboard-only task completion with visible focus and meaningful accessible names.
Pitfall: Request geolocation only after a clear user gesture.