Imri Persiado
Imri Persiado

Reputation: 1889

requestLocationUpdates generates location by wrong time

I'm trying to get locations from 2 different providers: network and GPS.

I'm using minTime 1 minute and minDistance 300 meters in requestLocationUpdates. With those parameters I don't expect to ever get more than one update per minute (per provider). The problem is, I am getting updates more frequently than that (more than 1 per minute). Why?

Here is some code to demonstrate:

    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);

Here is the 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) {
    }
};

UpdateUILocation:

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.

}

The doReverseGeocoding turn the location into text and call the handler:

    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
            }
        }
    };

I tested this application while driving (which means that the minDistance parameter is not a factor) and I received more than 1 location update per minute.

Here are the locations I received while testing (please ignore the locations since it's in hebrew, just look for the time): http://imrip.interhost.co.il/

Upvotes: 1

Views: 786

Answers (1)

Charlie Collins
Charlie Collins

Reputation: 8876

"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..."

That's simply not true, not pre-JellyBean anyway.

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.

Pre-JB you can get updates MORE FREQUENTLY than your min time specifies, especially if GPS reception is sketchy. See this answer for more details:

requestLocationUpdates interval in Android

Upvotes: 2

Related Questions