【问题标题】:Reduce the frequency of geolocation watchPosition calls减少地理定位 watchPosition 调用的频率
【发布时间】:2013-04-24 12:25:19
【问题描述】:
我正在为一家物流公司编写一个跟踪应用程序,该应用程序需要定期将每部手机的位置报告回基地。 geolocation.watchPosition 方法似乎很理想,但据我所知,它每秒更新一次位置,似乎无法增加回调之间的间隔。从数据使用的角度来看,每秒一次有点过于频繁 - 每分钟一次是理想的。
我可以将选项传递给 watchPosition 以使其以最小间隔执行回调吗?
【问题讨论】:
标签:
html
gps
w3c-geolocation
【解决方案1】:
您可以允许它仍然每秒调用一次 watchPosition,但随后将报告分离回基地到一个单独的回调中,该回调在计时器上每分钟运行一次。
例如:
var GPSStore = {};
navigator.geolocation.watchPosition(
function(current_location) {
// Just store the latest co-ordinates in memory as often as they are generated
GPSStore.location = current_location;
}
);
// Once a minute transmit the latest co-ordinates
setInterval(function() {
transmitLocation(GPSStore.location);
}, 1000*60);
【解决方案2】:
PositionOptions 有一个名为 maximumAge 的属性。
文档说:
是一个正长值,表示可接受返回的可能缓存位置的最大年龄(以毫秒为单位)。如果设置为 0,则意味着设备不能使用缓存的位置,必须尝试检索真实的当前位置。如果设置为 Infinity,则设备必须返回缓存位置,而不管其年龄。默认值:0。
link
它可以帮助你