Geolocation HTML5 enableHighAccuracy True, False or What?

The enableHighAccuracy property in HTML5 Geolocation API controls whether the device should use high-precision GPS or faster network-based location services.

Syntax

navigator.geolocation.getCurrentPosition(
    successCallback,
    errorCallback,
    {
        enableHighAccuracy: true,  // or false
        timeout: 10000,
        maximumAge: 0
    }
);

enableHighAccuracy: true

When set to true, requests the most accurate position possible, typically using GPS:

<script>
function getHighAccuracyLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                console.log("High accuracy - Latitude: " + position.coords.latitude);
                console.log("High accuracy - Longitude: " + position.coords.longitude);
                console.log("Accuracy: " + position.coords.accuracy + " meters");
            },
            function(error) {
                console.log("Error: " + error.message);
            },
            {
                enableHighAccuracy: true,
                timeout: 10000,
                maximumAge: 0
            }
        );
    }
}

getHighAccuracyLocation();
</script>

enableHighAccuracy: false

When set to false, allows faster, less accurate positioning using network-based methods:

<script>
function getFastLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                console.log("Fast mode - Latitude: " + position.coords.latitude);
                console.log("Fast mode - Longitude: " + position.coords.longitude);
                console.log("Accuracy: " + position.coords.accuracy + " meters");
            },
            function(error) {
                console.log("Error: " + error.message);
            },
            {
                enableHighAccuracy: false,
                timeout: 5000,
                maximumAge: 300000
            }
        );
    }
}

getFastLocation();
</script>

Handling Timeout Errors

If high accuracy mode times out, fall back to fast mode:

<script>
function getLocationWithFallback() {
    // Try high accuracy first
    navigator.geolocation.getCurrentPosition(
        function(position) {
            console.log("High accuracy success!");
            console.log("Latitude: " + position.coords.latitude);
            console.log("Longitude: " + position.coords.longitude);
        },
        function(error) {
            console.log("High accuracy failed: " + error.message);
            console.log("Trying fast mode...");
            
            // Fallback to fast mode
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    console.log("Fast mode success!");
                    console.log("Latitude: " + position.coords.latitude);
                    console.log("Longitude: " + position.coords.longitude);
                },
                function(error) {
                    console.log("Both methods failed: " + error.message);
                },
                { enableHighAccuracy: false, timeout: 5000 }
            );
        },
        { enableHighAccuracy: true, timeout: 10000 }
    );
}

getLocationWithFallback();
</script>

Comparison

Setting Accuracy Speed Battery Usage Works Indoors
true High (GPS) Slower Higher Poor
false Medium (Network) Faster Lower Better

Browser Compatibility

Both settings work across modern browsers including Chrome, Firefox, Safari, and mobile browsers on Android and iOS devices.

Conclusion

Use enableHighAccuracy: true for precise location needs, but implement fallback to false to handle timeout errors. The false setting provides faster, network-based positioning suitable for most web applications.

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

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements