我正在开发一个 Android 应用程序,我需要在其中显示位置更新,每秒更新约 30-40 次。
我正在使用Google Play 服务的位置 API http://developer.android.com/google/play-services/location.html,在 Google I/O 2013 中推出。它使用融合位置提供程序(利用加速计和其他传感器以及 GPS)来实现更准确、更高效的位置跟踪。
这是我的代码:
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
}
};
我在这里将优先级设置为 PRIORITY_HIGH_ACCURACY,并将间隔和最快间隔设置为 0 毫秒。但我仍然每 1 秒收到一次更新。
这看起来很像 GPS 传感器的更新频率
任何安卓手机。但我期待更多,因为这是使用 Fusion
传感器(包括加速度计)。加速度计是
融合传感器应该产生比这更高的频率。
我尝试了其他间隔值,但无法获取间隔时间小于 1 秒的更新。 ACCESS_FINE_LOCATION 还用于清单中的权限。
我在这里错过了什么吗?他们还有其他方法来实现这一目标吗?对于解决此问题的任何帮助,我将不胜感激。
目前您获取更新的速度不能超过每秒一次。这是硬件 GPS 限制。网络定位有点慢(在室内使用) - 目前大约每 5 秒一次。原始加速度计数据的频率较高,但直接集成时往往噪声很大。这可能在未来有所改善,但可能不会达到您想要的高频率。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)