javascript navigator.geolocation.watchPosition - 频率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24019286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:00:14  来源:igfitidea点击:

navigator.geolocation.watchPosition - frequency

javascripthtmlgeolocation

提问by mr.x

I tried this code :

我试过这个代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Location Location Location</title>

    <script type="text/javascript" charset="utf-8">

    var watchID = null;

    // PhoneGap is ready
    //
    function f() {
        // Update every 1 ms seconds
        var options = {enableHighAccuracy: true,timeout: 5000,maximumAge: 0,desiredAccuracy: 0, frequency: 1 };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var xmlhttp;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }       
        var str = 'Latitude: '  + position.coords.latitude      + '<br>' +
                  'Longitude: ' + position.coords.longitude     + '<br>' +
                  'Timestamp: ' + position.timestamp     + '<br>' ;
        var url = "load.php";
        var params = "data="+str;
        xmlhttp.open("GET", url+"?"+params, true);
        document.body.innerHTML += str;
        document.writeln("line 33");
        xmlhttp.send();
        //document.writeln("send");
        //document.writeln(str);
    }

    // clear the watch that was started earlier
    // 
    function clearWatch() {
        if (watchID != null) {
            navigator.geolocation.clearWatch(watchID);
            watchID = null;
        }
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
      alert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
    }

    </script>
  </head>
  <body>
    <p id="geolocation">Watching geolocation...</p>
    <button onclick="f();"> Watch</button>     
  </body>
</html>

you can see that i add

你可以看到我添加

var options = {......, frequency: 1 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

But I am getting new result if and only if I enter to tab and exit and enter agin.

但是当且仅当我进入 tab 并退出并输入 agin 时,我才会得到新的结果。

What can I do?

我能做什么?

回答by RamRaider

The spec makes no mention of a frequency option which suggests that this parameter is being ignored. W3C Geolocation spec

该规范没有提及频率选项,这表明该参数被忽略。W3C 地理定位规范

回答by Luizgrs

You code seems to be working fine: http://jsfiddle.net/Mgk9J/1/

你的代码似乎工作正常:http: //jsfiddle.net/Mgk9J/1/

I tested it in Android and when I move the cellphone new lines came up. However in chrome desktop it plots new lines when I change tabs even though they're identical to the last ones, according to the spec this isn't the correctly behavior.

我在 Android 上进行了测试,当我移动手机时,新线路出现了。但是,在 chrome 桌面中,即使它们与上一个相同,但当我更改选项卡时,它会绘制新行,根据规范,这不是正确的行为。

It makes a bit of sense for the desktop version do not plot new lines anyway, the computer isn't moving so new success calbacks execution should not be fired.

无论如何,桌面版本不绘制新行是有意义的,计算机没有移动,因此不应触发新的成功回调执行。

<html><head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type="text/javascript" src="/js/lib/dummy.js"></script>




  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type="text/css">

  </style>



<script type="text/javascript">//<![CDATA[ 
window.onload=function(){
    var watchID = null;

    // PhoneGap is ready
    //
    function f() {
        // Update every 1 ms seconds
        var options = {enableHighAccuracy: true,timeout: 5000,maximumAge: 0,desiredAccuracy: 0, frequency: 1 };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {

        var str = 'Latitude: '  + position.coords.latitude      + '<br>' +
                  'Longitude: ' + position.coords.longitude     + '<br>' +
                  'Timestamp: ' + position.timestamp     + '<br>\r\n' ;
        document.getElementById('result').value += str;
    }

    // clear the watch that was started earlier
    // 
    function clearWatch() {
        if (watchID != null) {
            navigator.geolocation.clearWatch(watchID);
            watchID = null;
        }
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
      alert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
    }

    document.getElementById('button').addEventListener('click', f);
}//]]>  

</script>


</head>
<body>
      <p id="geolocation">Watching geolocation...</p>
    <button id="button"> Watch</button>
    <textarea id="result" cols="100" rows="10"></textarea>






</body></html>