开发者

Geolocation hangs after x attemps, works after browser restart

开发者 https://www.devze.com 2023-01-28 08:58 出处:网络
I\'m working on a script that needs to grab my geolocation. It works most of the time, However every now and then the geolocation is not aquired and I have to restart the browser for it to work again.

I'm working on a script that needs to grab my geolocation. It works most of the time, However every now and then the geolocation is not aquired and I have to restart the browser for it to work again. This occurs in Safari and FF (Not tested in IE and Chrome). Does anybody know what could be causing this? I'm using this bit of code from the book "HTML 5".

<script type="text/javascript">

function loadDemo() {
    if(navigator.geolocation) {
        document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
        navigator.geolocation.getCurrentPosition(updateLocation);
    }
}

function updateLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    if (!latitude || !longitude) {
        document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser, but location is currently not available.";
        return;
    }

    document.getElementById("latitude").innerHTML = latitude;
    document.开发者_开发百科getElementById("longitude").innerHTML = longitude;
}


FF sometimes hangs, don't know about safari. It doesnt work in IE yet as far as i know. It seems to work great in Chrome so far. You can "solve" this by setting a timeout. It still doesn't work but at least the script is terminated after a while. I use this code. If you you found a good fix please let me know.

    navigator.geolocation.getCurrentPosition(
        function(position) {
            //succes handler
        },
        function errorCallback(error) {
            //error handler
        },
        {
            enableHighAccuracy:false,
            maximumAge:Infinity,
            timeout:5000
        }
    );
0

精彩评论

暂无评论...
验证码 换一张
取 消