我正在尝试从 2 个不同的提供商处获取位置:网络和 GPS。
我正在使用 minTime 1 分钟和 minDistance 300 米requestLocationUpdates
。使用这些参数,我不希望每分钟(每个提供商)获得超过一个更新。问题是,我获得更新的频率比这更高(每分钟超过 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) {
}
};
更新UI位置:
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/