使用用户位置进行运动检测无法正常工作

时间:2018-10-05 06:42:16

标签: java android

我已经通过计算我正在执行的googleAPIClient的用户经纬度获取,来实现我应用中用户的运动检测

1)每15秒获取用户位置

2)15秒之前和15秒之后的位置,我正在使用以下公式计算速度

`private double getSpeed(Location currentLocation, Location oldLocation) {
        if (currentLocation != null && oldLocation != null) {
            double newLat = currentLocation.getLatitude();
            double newLon = currentLocation.getLongitude();
            double oldLat = oldLocation.getLatitude();
            double oldLon = oldLocation.getLongitude();
            if (currentLocation.hasSpeed()) {
                return currentLocation.getSpeed();
            } else {
                double radius = 6371000;
                double dLat = Math.toRadians(newLat - oldLat);
                double dLon = Math.toRadians(newLon - oldLon);
                double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                        Math.cos(Math.toRadians(newLat)) * Math.cos(Math.toRadians(oldLat)) *
                            Math.sin(dLon / 2) * Math.sin(dLon / 2);
                double c = 2 * Math.asin(Math.sqrt(a));
                double distance = ((Math.round(radius * c)) * 36) /1.609344;    // convert meter to km as m*60*60/s*1000 thn convert whole into miles;
                CommonMethods.showLogs("BaseActivity.this", "getSpeed distance == " + distance);
                double timeDifferent = (currentLocation.getTime() - oldLocation.getTime()) * 10; //convert mili sec to hr
                CommonMethods.showLogs("BaseActivity.this", "getSpeed time == " + timeDifferent);
                return distance / timeDifferent;
           }
        }
        return 0;
    }`

3)之后,检查车速是否超过我的东西

4)在汽车和公共汽车中进行测试时工作正常,但在2中进行测试时工作正常 惠勒它有时无法正常工作

我没问题我的情况有什么问题,请帮忙!

谢谢!

0 个答案:

没有答案