2

I'm developing an android app in which I need to show location updates with some 30-40 updates per second. I'm using the Location APIs of Google Play Services, introduced in Google I/O 2013. It uses a fused location provider (making use of accelerometers and other sensors along with GPS) for more accurate & efficient location tracking.

Here's my code:

protected void startLocationTracking() {
    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this)) {
        mLocationClient = new LocationClient(this, mConnectionCallbacks, mConnectionFailedListener);
        mLocationClient.connect();
    }
}

private ConnectionCallbacks mConnectionCallbacks = new ConnectionCallbacks() {

    @Override
    public void onDisconnected() {
    }

    @Override
    public void onConnected(Bundle arg0) {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setFastestInterval(0);
        locationRequest.setInterval(0).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationClient.requestLocationUpdates(locationRequest, mLocationListener);
    }
};

private OnConnectionFailedListener mConnectionFailedListener = new OnConnectionFailedListener() {

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        Log.e(TAG, "ConnectionFailed");
    }
};

private LocationListener mLocationListener = new LocationListener() {

    private long mLastEventTime = 0;

    @Override
        public void onLocationChanged(Location location) {
            double delayBtnEvents = (System.nanoTime()- mLastEventTime )/(1000000000.0);
            mLastEventTime = System.nanoTime();

            //Sampling rate is the frequency at which updates are received
            String samplingRate = (new DecimalFormat("0.0000").format(1/delayBtnEvents));     

            float speed = (float) (location.getSpeed() * 3.6);  // Converting m/s to Km/hr
            tv.setText(speed + " kmph" + ", " + samplingRate + " Hz"); //Updating UI
    }
};

I have set priority as PRIORITY_HIGH_ACCURACY and the interval & the fastest interval as 0 milliseconds here. But I still receive updates on every 1 second only.

This pretty much looks like the update frequency of the GPS sensor of any Android phone. But I was expecting more as this is using a Fusion Sensor (which includes accelerometer). Accelerometer being part of fusion sensor should yield higher frequency than this.

I have tried other values for the interval, but could not get updates for less than 1 sec interval. Also ACCESS_FINE_LOCATION is used for permission in manifest.

Am I missing something here? Is their any other approach to accomplish this? I'll be grateful for any help to solve this.

Vinayak
  • 523
  • 1
  • 9
  • 20
  • 1
    I am not able to get onLocationChange method call every 1 seconds. I have set Interval and Fast Interval both to 0. But location get updates at 5 seconds only. I wan updated location every 1 seconds. Can you please kindly help as you already did this. – Scorpion Dec 05 '13 at 08:00
  • @Scorpion Did you set priority in locationRequest as PRIORITY_HIGH_ACCURACY ? You refer the above code in onConnected() callback method. – Vinayak Dec 05 '13 at 09:57
  • Yes, And I did the same thing described above. If you want to check my code then please refer this link. http://stackoverflow.com/questions/20372079/drawing-path-on-google-maps-using-google-maps-android-api-v2 – Scorpion Dec 05 '13 at 10:14
  • @Scorpion, where you able to get updates faster than 5 seconds? My Nexus 5 is only getting updates every 5 seconds. I'm wondering if I should not use the new(er) fused location provider and google play services and switch back to the old location provider where I can specify to only use gps. Will the old provider be deprecated at some point in time? – nickfox Dec 16 '14 at 22:27
  • My Nexus 6P is getting updates each 10 seconds... – Makalele Nov 06 '17 at 13:49

4 Answers4

3

Currently you can't get updates faster than once per second. This is the hardware gps limit. Network location is a bit slower (used when indoors) - currently about once every 5 seconds. Raw accelerometer data comes in at a higher frequency, but tends to be very noisy when integrated directly. This may improve in the future, but probably not at the high frequency you're looking for.

David
  • 749
  • 5
  • 2
1

I am trying to change with below parameter and its working for me with whatever I set.

locationRequest.setFastestInterval(10000); // Every 10 sec it gives lat/lng

Please try with in your side with same change.

PrashantAdesara
  • 1,897
  • 3
  • 22
  • 41
0

I think a problem is that you set PRIORITY_HIGH_ACCURACY. High_accuracy = GPS provider

Gps chipsets in phone usually have only ~1Hz update rate.

Maybe it can work if you disable HighAccuracy.

szpic
  • 4,346
  • 15
  • 54
  • 85
  • As per the documentation, it says that "Applications cannot specify the exact location sources, such as GPS, that are used by the LocationClient. In fact, the system may have multiple location sources (providers) running and may fuse the results from several sources into a single Location object." Other options are PRIORITY_BALANCED_POWER_ACCURACY and PRIORITY_NO_POWER. In the former case it has 100 meter accuracy (which is too much for my use) and latter makes the app passive listener to locations accessed by other apps.I'm stuck with PRIORITY_HIGH_ACCURACY. – Vinayak May 24 '13 at 13:26
-5

The fact is there is a hardware delay that you can't ignore. Try to pass a float to your interval. Kill zombies after each update may help too.

Michele
  • 681
  • 3
  • 16
  • 27