navigator.geolocation.watchPosition - frequency
20,382
Solution 1
The spec makes no mention of a frequency option which suggests that this parameter is being ignored. W3C Geolocation spec
Solution 2
You code seems to be working fine: 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.
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>
Author by
mr.x
Updated on August 12, 2020Comments
-
mr.x over 3 years
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.
What can I do?
-
mr.x almost 10 yearstanx but I want to measure changes between two successive measurements even there is no movem
-
Luizgrs almost 10 yearsThe W3C specification clearly says that watchPosition should only callback if the poisition changes. I suggest you to rethink your approach.