Error codes returned in the PositionError object HTML5 Geolocation

The HTML5 Geolocation API returns specific error codes through the PositionError object when location requests fail. Understanding these codes helps you handle different error scenarios appropriately.

Error Codes Reference

The following table describes the possible error codes returned in the PositionError object:

Code Constant Description
0 UNKNOWN_ERROR The method failed to retrieve the location due to an unknown error.
1 PERMISSION_DENIED The application does not have permission to use the Location Service.
2 POSITION_UNAVAILABLE The location of the device could not be determined.
3 TIMEOUT Unable to retrieve location information within the specified timeout interval.

Example: Error Handling

Here's how to handle different error codes in your error callback function:

<script>
function errorHandler(err) {
    switch(err.code) {
        case err.PERMISSION_DENIED:
            console.log("Error: Access denied by user");
            break;
        case err.POSITION_UNAVAILABLE:
            console.log("Error: Location information unavailable");
            break;
        case err.TIMEOUT:
            console.log("Error: Request timeout");
            break;
        case err.UNKNOWN_ERROR:
            console.log("Error: Unknown error occurred");
            break;
        default:
            console.log("Error: Unexpected error code");
    }
}

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                console.log("Location found: " + position.coords.latitude + ", " + position.coords.longitude);
            },
            errorHandler
        );
    } else {
        console.log("Geolocation not supported");
    }
}

// Trigger location request
getLocation();
</script>

Complete Example with Error Messages

This example demonstrates a complete implementation with user-friendly error messages:

<script>
function showLocationError(error) {
    const errorMessages = {
        1: "Location access denied. Please enable location permissions.",
        2: "Location unavailable. Check your GPS or network connection.",
        3: "Location request timed out. Please try again.",
        0: "An unknown error occurred while retrieving location."
    };
    
    const message = errorMessages[error.code] || "Unexpected error";
    console.log("Geolocation Error (" + error.code + "): " + message);
}

// Request location with error handling
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        function(position) {
            console.log("Success! Latitude: " + position.coords.latitude + 
                       ", Longitude: " + position.coords.longitude);
        },
        showLocationError,
        { timeout: 10000, enableHighAccuracy: true }
    );
}
</script>

Key Points

  • Code 1 (PERMISSION_DENIED) - Most common error, occurs when user blocks location access
  • Code 2 (POSITION_UNAVAILABLE) - GPS/network issues prevent location detection
  • Code 3 (TIMEOUT) - Request exceeds specified timeout duration
  • Code 0 (UNKNOWN_ERROR) - Catch-all for unexpected failures

Conclusion

Proper error handling with PositionError codes ensures your geolocation features gracefully handle failures. Always provide meaningful feedback to users based on the specific error type encountered.

Updated on: 2026-03-15T23:18:59+05:30

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements