Search code examples
javascriptgeolocation

JavaScript geolocation sporadic in IE


I have a problem very similar to this one, however figured that it was different in a particularly fundamental way enough to warrant its own question. I have a page which requests the user's location once every 2 seconds (very frequent, I know, but it's for a purpose) using getCurrentPosition(). Here's my code:

function locationWrapper() {
    if (!navigator.geolocation) {
        success = false;
        console.log("Geolocation is not supported by this browser")
    }
    if (success) {
        navigator.geolocation.getCurrentPosition(
            recordPosition, catchError, { enableHighAccuracy: false }
        );
    }
}

The recordPosition() function doesn't really do anything pertinent (and is rarely ever reached) to the question. catchError looks like this:

function catchError(error) {
    console.log(error);
    switch (error.code) {
        case error.PERMISSION_DENIED:
            console.log("User denied the request for Geolocation.");
        case error.POSITION_UNAVAILABLE:
            console.log("Location information is unavailable.");
            break;
        case error.TIMEOUT:
            console.log("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
            console.log("An unknown error occurred.");
            break;
    }
}

All fairly straightforward. This works perfectly in Chrome, and only sometimes in IE. That's the baffling thing. It's called once at the very start of the JS document and then once every 2 seconds by a setInterval() function. The first one always works, and in the interval it works roughly once in 20 times, but otherwise returns the incredibly helpful POSITION_UNAVAILABLE error.

I have tried:

  • Changing the enableHighAccuracy option in getCurrentPosition() to be false, as can be seen in my code

  • Ensuring that there are not any issues with the permissions in IE

  • Extending the interval period to 5 seconds

Any input is much appreciated.

EDIT:

It's specifically IE11. Sorry for missing that out.

EDIT 2:

I'm seeing the same behaviour using this example. The first time works (displays coordinates) but afterwards just shows an error until I reload.


Solution

  • So this appears to be a problem with IE that other users are also having. There is a question about it on the Edge and IE support page, but it's been a bit muddied by the OP stating that Edge is also not working (which has performed fine for me) and seems to have been ignored. I have made a comment on the thread and if nothing comes of it I may have to make my own question relating only to IE.

    Seems like a pretty big deal to me - if developers can't use getCurrentLocation() when they need to, their sites just won't work properly in IE. I hope Microsoft take an interest.