请求位置更新的频率超过 5 秒(Android 融合位置)
Requesting location updates more often than 5 seconds (Android Fused location)
我正在制作一个实时位置侦听应用程序,我需要每 3 秒 调用一次位置更新。
我使用Fused位置。
我的间隔设置为 3 秒,但看起来此间隔的最小值是 5 秒。
代码:
public class GpsService extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static int UPDATE_INTERVAL = 1000 * 3; // /< location update interval
private static int FASTEST_INTERVAL = 1000 * 3; // /< fastest location update interval
private static int DISPLACE_METERS = 10; // displacement in meters, not used atm
private LocationRequest mLocationRequest;
private LocationWrapper mLastLocation;
...
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
...
@Override
public void onLocationChanged(Location location) {
mLastLocation = new LocationWrapper(location);
log("onLocationChanged" + " " + mLastLocation.toString());
}
Logcat:
03-11 14:35:41.559: I/Locationing_GpsService(7410): Lat: 47.513878
03-11 14:35:41.559: I/Locationing_GpsService(7410): Lng: 19.0362011
03-11 14:35:41.559: I/Locationing_GpsService(7410): Accuracy: 21.213 meters
03-11 14:35:41.559: I/Locationing_GpsService(7410): ===================
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lat: 47.5138783
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lng: 19.0362064
03-11 14:35:46.554: I/Locationing_GpsService(7410): Accuracy: 17.32 meters
03-11 14:35:46.554: I/Locationing_GpsService(7410): ===================
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lat: 47.5138752
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lng: 19.0362068
03-11 14:35:51.569: I/Locationing_GpsService(7410): Accuracy: 15.0 meters
可以看到logcat的日期,延迟5秒:41,46,51
我已经阅读了 Google 关于 Fused location 的文档,但我没有找到任何关于间隔最小值的信息。
有什么建议吗?
编辑:
添加getTime()
记录,解析为Date
对象,可以更清楚5秒延迟
03-11 15:16:01.851: I/Locationing_GpsService(16747): Lat: 47.5138798
03-11 15:16:01.851: I/Locationing_GpsService(16747): Lng: 19.0362125
03-11 15:16:01.851: I/Locationing_GpsService(16747): Accuracy: 28.713 meters
03-11 15:16:01.851: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:01 CET 2015
03-11 15:16:01.851: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lat: 47.5138765
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lng: 19.0361987
03-11 15:16:06.946: I/Locationing_GpsService(16747): Accuracy: 20.902 meters
03-11 15:16:06.946: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:06 CET 2015
03-11 15:16:06.946: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lat: 47.5138767
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lng: 19.0361916
03-11 15:16:11.971: I/Locationing_GpsService(16747): Accuracy: 17.326 meters
03-11 15:16:11.971: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:11 CET 2015
03-11 15:16:11.971: I/Locationing_GpsService(16747): =========================================================
编辑 2:
权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
运行 您在 Activity 中将间隔设置为 0 的代码在 Nexus 4、5 和 7 上以每秒 1 次的速度生成更新...就像使用 LocationManager 一样。您可能会受到设备硬件的限制。您是否刚刚尝试使用 LocationRequest 和 LocationManager 请求最快的更新间隔?如果它们都被限制为 5 秒,我会怀疑这是硬件特定的设备。
位置请求:
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(0);
mLocationRequest.setFastestInterval(0);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Logcat:
03-11 09:50:26.931: D/MainActivity(5623): onLocationChanged Location[fused 0,0 acc=12 et=+1d17h57m7s979ms alt=1505.0 vel=0.0]
03-11 09:50:27.514: D/MainActivity(5623): onLocationChanged Location[fused 0,0, acc=12 et=+1d17h57m8s571ms alt=1505.0 vel=0.0]
编辑:
Docs 还告诉您 setInterval(long) 可以以指定的更慢或更快的速率进行更新。
我正在制作一个实时位置侦听应用程序,我需要每 3 秒 调用一次位置更新。
我使用Fused位置。
我的间隔设置为 3 秒,但看起来此间隔的最小值是 5 秒。
代码:
public class GpsService extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static int UPDATE_INTERVAL = 1000 * 3; // /< location update interval
private static int FASTEST_INTERVAL = 1000 * 3; // /< fastest location update interval
private static int DISPLACE_METERS = 10; // displacement in meters, not used atm
private LocationRequest mLocationRequest;
private LocationWrapper mLastLocation;
...
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
...
@Override
public void onLocationChanged(Location location) {
mLastLocation = new LocationWrapper(location);
log("onLocationChanged" + " " + mLastLocation.toString());
}
Logcat:
03-11 14:35:41.559: I/Locationing_GpsService(7410): Lat: 47.513878
03-11 14:35:41.559: I/Locationing_GpsService(7410): Lng: 19.0362011
03-11 14:35:41.559: I/Locationing_GpsService(7410): Accuracy: 21.213 meters
03-11 14:35:41.559: I/Locationing_GpsService(7410): ===================
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lat: 47.5138783
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lng: 19.0362064
03-11 14:35:46.554: I/Locationing_GpsService(7410): Accuracy: 17.32 meters
03-11 14:35:46.554: I/Locationing_GpsService(7410): ===================
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lat: 47.5138752
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lng: 19.0362068
03-11 14:35:51.569: I/Locationing_GpsService(7410): Accuracy: 15.0 meters
可以看到logcat的日期,延迟5秒:41,46,51 我已经阅读了 Google 关于 Fused location 的文档,但我没有找到任何关于间隔最小值的信息。
有什么建议吗?
编辑:
添加getTime()
记录,解析为Date
对象,可以更清楚5秒延迟
03-11 15:16:01.851: I/Locationing_GpsService(16747): Lat: 47.5138798
03-11 15:16:01.851: I/Locationing_GpsService(16747): Lng: 19.0362125
03-11 15:16:01.851: I/Locationing_GpsService(16747): Accuracy: 28.713 meters
03-11 15:16:01.851: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:01 CET 2015
03-11 15:16:01.851: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lat: 47.5138765
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lng: 19.0361987
03-11 15:16:06.946: I/Locationing_GpsService(16747): Accuracy: 20.902 meters
03-11 15:16:06.946: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:06 CET 2015
03-11 15:16:06.946: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lat: 47.5138767
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lng: 19.0361916
03-11 15:16:11.971: I/Locationing_GpsService(16747): Accuracy: 17.326 meters
03-11 15:16:11.971: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:11 CET 2015
03-11 15:16:11.971: I/Locationing_GpsService(16747): =========================================================
编辑 2:
权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
运行 您在 Activity 中将间隔设置为 0 的代码在 Nexus 4、5 和 7 上以每秒 1 次的速度生成更新...就像使用 LocationManager 一样。您可能会受到设备硬件的限制。您是否刚刚尝试使用 LocationRequest 和 LocationManager 请求最快的更新间隔?如果它们都被限制为 5 秒,我会怀疑这是硬件特定的设备。
位置请求:
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(0);
mLocationRequest.setFastestInterval(0);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Logcat:
03-11 09:50:26.931: D/MainActivity(5623): onLocationChanged Location[fused 0,0 acc=12 et=+1d17h57m7s979ms alt=1505.0 vel=0.0]
03-11 09:50:27.514: D/MainActivity(5623): onLocationChanged Location[fused 0,0, acc=12 et=+1d17h57m8s571ms alt=1505.0 vel=0.0]
编辑: Docs 还告诉您 setInterval(long) 可以以指定的更慢或更快的速率进行更新。