gpt4 book ai didi

java - requestLocationUpdates 在错误的时间生成位置

转载 作者:行者123 更新时间:2023-11-29 21:51:02 27 4
gpt4 key购买 nike

我正在尝试从 2 个不同的提供商处获取位置信息:网络和 GPS。

我在 requestLocationUpdates 中使用 minTime 1 分钟和 minDistance 300 米。使用这些参数,我不希望每分钟(每个提供商)获得超过一个更新。问题是,我获取更新的频率更高(每分钟超过 1 个)。为什么?

下面是一些代码来演示:

    mLocationManager.removeUpdates(listener);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);

if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);

这是监听器:

private final LocationListener listener = new LocationListener() 
{

@Override
public void onLocationChanged(Location location)
{
if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER))
updateUILocation(location,LocationService.this.gpsLocation);
else
updateUILocation(LocationService.this.networkLocation, location);
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

更新UILocation:

private void updateUILocation(Location networkLocation,Location gpsLocation) 
{
Location location = null;
if(gpsLocation == null || gpsLocation.getAccuracy() > 800)
{
if(!(networkLocation == null || networkLocation.getAccuracy() > 800))
location = networkLocation;
}
else
if(networkLocation == null)
{
if(gpsLocation.getAccuracy() < 800)
location = gpsLocation;
}
else
if(gpsLocation.getAccuracy() < networkLocation.getAccuracy() && gpsLocation.getAccuracy() < 500)
location = gpsLocation;
else
if(networkLocation.getAccuracy() < 800)
location = networkLocation;
if(location!=null)
{
if (mGeocoderAvailable)
doReverseGeocoding(location);
}

// Bypass reverse-geocoding only if the Geocoder service is available on the device.

}

doReverseGeocoding 将位置转换为文本并调用处理程序:

    mHandler = new Handler() 
{
public void handleMessage(Message msg)
{
if(msg.what == UPDATE_ADDRESS) // msg.what == 1
{
LocationService.this.address = (String) msg.obj;
new SendLocation(LocationService.this.id,(String)msg.obj); //send the location to the db
LocationService.this.gpsLocation = null; //reset gps value
LocationService.this.networkLocation = null; //reset network value
}
}
};

我在开车时测试了这个应用程序(这意味着 minDistance 参数不是一个因素)并且我每分钟收到超过 1 个位置更新。

以下是我在测试时收到的位置(请忽略这些位置,因为它是希伯来语,只需查找时间):http://imrip.interhost.co.il/

最佳答案

"The minTime is 1 minute and the minDistance is 300 meter. With those parameters I never should get 2 locations in less then 1 minute..."

这根本不是真的,无论如何都不是 pre-JellyBean。

http://developer.android.com/reference/android/location/LocationManager.html

Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.

在 JB 之前,您可以获得比您指定的最短时间更频繁的更新,尤其是在 GPS 接收信号不佳的情况下。有关详细信息,请参阅此答案:

requestLocationUpdates interval in Android

关于java - requestLocationUpdates 在错误的时间生成位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415607/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com