LocationManager requestLocationUpdates minTime或minDistance [英] LocationManager requestLocationUpdates minTime OR minDistance
问题描述
我使用Android的 LocationManager
及其方法 requestLocationUpdates
像这样:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
当我发现minTime和minDistance的条件必须满足位置更新时,需要每3秒或10米更新一次。
我尝试了2次这样的要求:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10,this);
但它不起作用,我只用此代码每10米更新一次,而不是每3秒。
我正在使用API19,如果这很重要的话。
< requestLocationUpdate()表示:
$ b
requestLocationUpdates(String provider,long minTime,float
minDistance,LocationListener listener)
使用指定提供者和未决意图注册位置更新。
<所以你应该像 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
<但是,如果您将minTime设置为0,它将在第一次接收到位置更新时被调用一次,然后直到您在minDistance米中更改位置后才会调用它。
编辑
根据与@Matej的讨论我需要每隔10米更新一次,即使它发生在不到3秒的时间内,即使该位置的变化不超过10也每3秒更新一次米
如果要定期 requestLocationUpdates
,则应该使用 Timer 和
TimerTask
并且有 requestLocationUpdates
每3秒运行一次
schedule(TimerTask任务,长延迟,长周期)
为重复固定延迟执行,在指定延迟后开始
。
I'm using Android's LocationManager
and its method requestLocationUpdates
like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
As I found out both the conditions of minTime and minDistance have to be met for location update, but I need to get update every 3 seconds or 10 meters.
I tried to put 2 requests like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
but it doesn't work, I only get update every 10 meters with this code, not every 3 seconds.
I'm working with API19 if that matters.
The Documentation on requestLocationUpdate() says :
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
Register for location updates using the named provider, and a pending intent.
So you should be calling it like locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
But If you set minTime to 0, it will be called once when it first receives a location update, then it won't be called until you change your position in minDistance meters.
Documentation Link for Reference
EDIT
As per the Discussion with @Matej I need to get update every 10 meters even if it happened in less than 3 seconds, and update every 3 seconds even if the location didn't change by more than 10 meters
If you want to regularly requestLocationUpdates
, you should use Timer
and TimerTask
and have requestLocationUpdates
run once every 3 seconds
schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
这篇关于LocationManager requestLocationUpdates minTime或minDistance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!