移动时速度保持为0

时间:2016-07-25 12:24:38

标签: android location

我目前正试图在我的应用程序中获得用户当前的速度。

我已经设法连接到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未正确设置?或完全是另一回事?

0 个答案:

没有答案