LocationRequest in Google Play Services isn't

2020-07-24 06:21发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.