java Google Play 服务中的 LocationRequest 不会在设置为小于 1 秒的间隔内更新。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16713659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:45:22  来源:igfitidea点击:

LocationRequest in Google Play Services isn't updating for interval set as less than 1 second.

javaandroidgpslocationaccelerometer

提问by Vinayak

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.

我正在开发一个 android 应用程序,我需要在其中以每秒 30-40 次更新显示位置更新。我正在使用Google Play Services位置 API,在 Google I/O 2013 中引入。它使用融合位置提供程序(利用加速度计和其他传感器以及 GPS)进行更准确和高效的位置跟踪。

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.

我在这里将优先级设置为 PRIORITY_HIGH_ACCURACY,并将间隔和最快间隔设置为 0 毫秒。但我仍然每 1 秒收到一次更新。

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.

这很像任何 Android 手机 GPS 传感器的更新频率。但我期待更多,因为这是使用融合传感器(包括加速度计)。作为融合传感器一部分的加速度计应该产生比这更高的频率。

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.

我已经尝试了间隔的其他值,但无法在少于 1 秒的间隔内获得更新。ACCESS_FINE_LOCATION 也用于清单中的权限。

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

我在这里错过了什么吗?他们还有其他方法可以做到这一点吗?对于解决此问题的任何帮助,我将不胜感激。

回答by David

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.

目前,您无法以每秒一次的速度获得更新。这是硬件 GPS 限制。网络位置有点慢(在室内使用) - 目前大约每 5 秒一次。原始加速度计数据以更高的频率进入,但直接集成时往往会非常嘈杂。这在未来可能会有所改善,但可能不会达到您正在寻找的高频。

回答by PrashantAdesara

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.

请尝试在您身边进行相同的更改。

回答by szpic

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

我认为问题在于您设置了 PRIORITY_HIGH_ACCURACY。High_accuracy = GPS 提供商

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

手机中的 Gps 芯片组通常只有 ~1Hz 的更新率。

Maybe it can work if you disable HighAccuracy.

如果您禁用 HighAccuracy,也许它可以工作。

回答by Michele

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.

事实是存在您无法忽视的硬件延迟。尝试将浮点数传递给您的间隔。每次更新后杀死僵尸也可能有帮助。