我想制作一个运行应用程序,用于跟踪用户位置、速度和运行距离。目前我正在使用Google Location Services API来获取GPS数据,因为Google在the official site上推荐它。
但问题是我只能每 10 秒接收一次位置更新,如果只涉及在 map 上绘制位置,这是可以的,但我想计算用户当前速度(公里/小时)、当前配速(分钟/米)并计算他跑一圈(1 公里或 1 英里)的确切时间。如果我每 10 秒更新一次,我就无法精确计算这些参数,这就是问题所在。例如,当用户处于 996m 时更新,而在 4m 时他将跑一圈。但下一个更新将在 10 秒内可用,因此它将在例如 10 秒内出现。 1010米。这不是用户想要的......
如何解决这个问题?你觉得怎么样?
I am using the exact code form here.
位置请求如下所示:
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
来自docs :
The priority of PRIORITY_HIGH_ACCURACY, combined with the ACCESS_FINE_LOCATION permission setting that you've defined in the app manifest, and a fast update interval of 5000 milliseconds (5 seconds), causes the fused location provider to return location updates that are accurate to within a few feet. This approach is appropriate for mapping apps that display the location in real time.
但这还不够。即使我将间隔更改得更小,它始终是 10 秒。
最佳答案
使用此代码可以每 3 秒发送一次位置更新。
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//This will be called when location is changed
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, listener);
此代码的更多详细信息 Github.
关于java - 如何利用GPS精确计算速度和距离?跟踪用户位置。用于运行的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015181/