我在快速更新 GPS 位置时遇到了一些麻烦,我需要每 100 毫秒更新一次位置。在我的解决方案中,我观察到 GPS 位置将每秒更新一次,而不是更快:( 我尝试使用 setInterval:
function localize(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position){
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
},function(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}, { maximumAge:100, timeout:100, enableHighAccuracy:true });
}else{
handleNoGeolocation(false);
}
}
localize();
setInterval(localize, 100);
}
使用 setTimeout:
localize();
function localize(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position){
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
},function(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}, { maximumAge:100, timeout:100, enableHighAccuracy:true });
setTimeout(localize, 100);
}else{
handleNoGeolocation(false);
}
}
setInterval(localize, 100);
}
或whatchPosition:
localize();
function localize(){
if(navigator.geolocation)
{
navigator.geolocation.watchPosition(function(position){
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
},function(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}, { maximumAge:100, timeout:100, enableHighAccuracy:true });
setTimeout(localize, 100); //with or without
}else{
handleNoGeolocation(false);
}
}
}
我尝试过使用或不使用 maximumAge 和 timeout 选项。我的设备是 iPhone 5。