我正在开发应用程序,它必须获取纬度和经度。就我而言, requestLocationUpdates (Stringprovider, long minTime, float minDistance, LocationListenerlistener)
不在选项中。为什么?因为我必须在用户执行需要该位置的操作时获取位置(假设他点击按钮)。我需要在按下按钮时准确定位。在 requestLocationUpdates
中,我们可以设置 minTime,如果我将其设置为 30000,那么“按钮点击时间”的位置将不会像我想要的那样好。出于同样的原因,minDistance 也没有我想要的那么好。如果我理解函数 requestLocationUpdates
正确的话,当 minTime 和 minDistance 设置为 0 时,位置会一直更新。如果我错了,请纠正我。该应用程序适用于我正在工作的公司,该应用程序将日夜使用。因此,如果应用程序始终检查位置更新,电池通常会耗尽。这就是为什么我在按下该按钮时需要位置。我发现requestSingleUpdate 函数。但我不能在 eclipse =( 中使用它,就像它不存在一样。
还有其他方法可以做到这一点还是我错过了什么?
i'm working on app, which must get latitude and longitude. in my case requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
is not in option. why? because i must get location just when user do something that location is needed (let's say he hits button). and i need location exactly on the time, when button is hit. in requestLocationUpdates
, we can set minTime-if i set this let's say on 30000 the location at the "button hit time" won't be as good as i want. of the same reason minDistance is also not as good as i want. if i understant function requestLocationUpdates
correct-when minTime and minDistance are set to 0, location is updating all the time. please correct me if i'm wrong. app is for company that i'm working at and that app will be used through the day and night. so if app will check for location updates all the time, battery would be often empty. thst's why i need location just at the time, that button is hit. i found requestSingleUpdate function. but i can't use it in eclipse =( like it doesn't exists.
is there some other way to do this or am i missing something?
发布评论
评论(2)
requestSingleLocation 方法。如果您的目标是较早的 API 级别,则此方法将不可用。
获取位置信息的问题之一是修复可能需要时间。这更多是 GPS 定位提供商的问题,而不是网络提供商的问题。如果您仅使用网络提供商,那么当用户单击按钮时获取位置信息可能不会有太大的延迟(我说这对您的应用程序如何处理数据或您的用户体验类型一无所知)正在努力提供)。
您还可以尝试的其他方法是 PASSIVE_PROVIDER。它允许您获取其他应用程序请求的位置更新。这意味着您必须请求 ACCESS_FINE_LOCATION 权限。您可能还需要运行某些程序来接收位置更新(可能是服务或 广播接收器)。 BroadcastReceiver 方法不应该使用太多额外的电池寿命,因为它仅在接收到 Intent 时才运行,而 PASSIVE_PROVIDER 仅在其他应用程序请求时才获取位置。
PASSIVE_PROVIDER 的另一个棘手问题是您最终应该调用 删除更新。在何时何地调用它实际上取决于您的应用程序的结构以及您如何处理用户退出应用程序。
The requestSingleLocation method new for API level 9. If you are targeting an earlier API level, this method will not be available to you.
One of the problems with obtaining location information is that it can take time to get a fix. This is more of a problem with the GPS location provider than the network provider. If you are only using the network provider, then there may not much of a delay with getting the location information when the user clicks the button (I say this knowing nothing about what your app does with the data, or what type of user experience you are trying to provide).
Something else you can try is the PASSIVE_PROVIDER. It allows you to get location updates that were requested from other apps. This will mean that you have to request the ACCESS_FINE_LOCATION permission however. You may also need to leave something running to receive the location updates (likely a service or a BroadCastReceiver). The BroadcastReceiver approach should not use that much additional battery life since it only runs when the Intent is received along with the PASSIVE_PROVIDER only getting locations when some other app requested them anyways.
The other sticky point with the PASSIVE_PROVIDER is that you should eventually call removeUpdates. Where and when to call this really depends on how your app is structured and how you handle the user exiting the app.
您可以使用 getLastKnownLocation 方法,但它可能已经过时了。 requestLocationUpdates 是异步的,因为 GPS 需要“预热”才能找到新位置。
requestSingleUpdate 方法自 API 级别 9 起可用,但您可以使用 requestLocationUpdates 来实现它,并在第一次回调 LocationListener 后禁用更新。
You can use getLastKnownLocation method, but it can be out-of-date. requestLocationUpdates is asynchronous because gps needs to "warm-up" to find new location.
requestSingleUpdate method is available since api level 9, but you can implements it's using requestLocationUpdates and disabling updates after first callback to LocationListener.