我目前正试图在我的应用程序中获得用户当前的速度。
我已经设法连接到GooglePlay服务,它显然正在更新,但我认为它正在不正确地计算速度,因为即使在移动应用程序时显示人的速度为零。
我当前的代码有什么问题,我该如何解决?
public void onLocationChanged(Location location) {
float Distance = OldLocation.distanceTo(location);
long TimeDiff = location.getTime()-OldLocation.getTime();
long SecondDiff = TimeUnit.MILLISECONDS.toSeconds(TimeDiff);
float Speed = Distance/SecondDiff;
OldLocation = location;
}
我在OldLocation
;
onConnected()
public void onConnected(Bundle connectionHint) {
Toast.makeText(this, "You have connected", Toast.LENGTH_LONG).show();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(UPDATE_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
OldLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
}
}
确切的问题是什么?应用程序是否只是没有正确获取位置?或者是因为全局变量OldLocation
未正确设置?或完全是另一回事?