Quiz Entry - updated: 2026.07.14
How do you handle errors when requesting geolocation?
Pass an error callback as the second argument to getCurrentPosition() and branch on error.code to handle permission denial, unavailable position, or timeout.
navigator.geolocation.getCurrentPosition(
// Success callback
showPosition,
// Error callback
handleError
);
function handleError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("User denied location access");
break;
case error.POSITION_UNAVAILABLE:
alert("Location unavailable");
break;
case error.TIMEOUT:
alert("Request timed out");
break;
}
}
Error codes:
| Code | Constant | Meaning |
|---|---|---|
| 1 | PERMISSION_DENIED |
User denied the request |
| 2 | POSITION_UNAVAILABLE |
Location unavailable (no GPS/WiFi) |
| 3 | TIMEOUT |
Request took too long |