【问题标题】:navigator.geolocation.watchPosition - frequencynavigator.geolocation.watchPosition - 频率
【发布时间】:2014-06-03 15:37:56
【问题描述】:

我试过这段代码:

<!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>

你可以看到我添加了

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

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

我能做什么?

【问题讨论】:

  • prollygeek 不,这不是科尔多瓦
  • 那是什么,这是什么api?
  • 其实我真的不知道,我一直在寻找,这就是我找到的
  • 地理位置 watchPosition 实际上有频率选项吗??
  • @ProllyGeek,就是 HTML5。

标签: javascript html geolocation


【解决方案1】:

规范没有提到频率选项,这表明该参数被忽略。 W3C Geolocation spec

【讨论】:

    【解决方案2】:

    您的代码似乎运行良好:http://jsfiddle.net/Mgk9J/1/

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

    桌面版本有点意思,反正不要绘制新行,计算机没有移动,所以不应该触发新的成功回调执行。

    <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>
    

    【讨论】:

    • tanx 但我想测量两次连续测量之间的变化,即使没有移动
    • W3C 规范明确规定 watchPosition 只应在位置发生变化时回调。我建议你重新考虑你的方法。
    猜你喜欢
    • 2023-03-21
    • 2012-11-16
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多