Android问题找出最近最新的GPS修复如何 [英] Android problem finding out how recent latest GPS fix is

查看:21
本文介绍了Android问题找出最近最新的GPS修复如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用使用 LocationListener 来跟踪当前位置.只要 GPS 提供商提供定期修复,它就可以正常工作.但是,我希望我的应用程序在位置不再可靠时提醒用户,因为修复不再是最新的.因此,我使用 timeCheckHandler 每隔几秒调用一次 getLastKnownLocation.

My app uses LocationListener to keep track of the current location. So long as the GPS Provider is providing regular fixes this works well. However, I want my app to alert the user if the location is no longer reliable because the fix is no longer current. I have therefore used a timeCheckHandler to call getLastKnownLocation every few seconds.

我的问题是,即使经常收到准确的修复,通过将 getTime() 应用于 getLastKnownLocation 返回的位置返回的时间通常比 System.currentTimeMillis() 返回的当前时间早,通常大约 20 秒.

My problem is that even when accurate fixes are being received frequently the time returned by applying getTime() to the location returned by getLastKnownLocation is generally older than the current time returned by System.currentTimeMillis(), often by about 20 seconds.

我通过向 onLocationChanged(arg0) 添加代码以记录修复时间 (arg0.getTime()) 和当前时间 (System.currentTimeMillis()) 进一步调查了该问题.我再次看到大约 20 秒的差异.

I have investigated the problem further by adding code to onLocationChanged(arg0) to log the time of the fix (arg0.getTime()) and the current time (System.currentTimeMillis()). Again I see a difference of about 20 seconds.

目前代码如下:

    public void onLocationChanged(Location arg0) {
    mapview.handleLocationChanged(mapview, arg0.getLatitude(), arg0.getLongitude(), arg0.getBearing(), arg0.getAccuracy(), "GPS fix");
    addDebugNote("Fix received at time: "+Long.toString(arg0.getTime()/1000)+" Now: "+Long.toString((System.currentTimeMillis())/1000));
}

我的调试文件的典型输出为:

and typical output to my Debug file reads:

Fix received at time: 1292091908 Now: 1292091928

为什么我会看到修复时间和当前系统时间之间的这种差异?

Why should I be seeing this difference between the fix time and the current system time?

我必须接受大约 20 秒的差异是正常的吗?

Do I have to accept that a difference of around 20 seconds is normal?

推荐答案

GPS 定位时间独立于您的网络提供商时间/设备时间.System.currentTimeMillis() 将为您提供设备上设置的设备时间.

GPS location time comes independently of your network provider time/device time. System.currentTimeMillis() will give you device time set on your device.

如果您想知道该点有多近,您可以:

If you want to know how recent the point is you can:

  1. 在应用程序启动时同步代码中的两个时间(GPS 和设备),方法是在您获得第一次 GPS 位置更新后立即获取两者之间的差异.在那个即时查询设备时间,看看两者有什么区别.将此差异保存在变量中.

  1. Synchronize both the times ( GPS and device ) in your code at application start by taking the difference between both as soon as you get first GPS location update. At that instant query device time and see what's the difference in both. Save this difference in variable.

在随后的位置更新中将此作为校正因子,以根据您需要的参考系了解准确时间.(设备时间或 GPS)

Use this as a correction factor in subsequent location updates to know the exact time based on the reference frame you need. ( Device time or GPS)

我还发现,使用 NETWORK 作为位置提供者,您可能只能获得设备时间.因此,如果您同时监听来自(GPS 和网络)的更新,您可能还需要使用 location_obj.getProvider() 来区分这一点并过滤掉 GPS 提供者.

Also I had found that using NETWORK as location provider you may get device time only. So if you are listening on updates from both ( GPS and network ) , you may also need to distinguish this using location_obj.getProvider() and filter out GPS provider.

这篇关于Android问题找出最近最新的GPS修复如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆