如何使用位置或加速度计或其他方式在 android 应用程序中获得速度加速度计、如何使用、应用程序、速度

2023-09-07 04:27:12 作者:不喊痛、不一定没感觉

I am working on app and try to get the speed and distance travelled by the user. I have used Google Play services location class to get the speed but it always returns me 0.0 value and not at all reliable. I wan accurate speed and distance travelled at real time.

I have installed GPS Speedometer app on my device and its so perfect that even if i am walking then it gives me the speed. I want to get the same thing. I am confused in how to get speed, using location or using accelerometer or is there any other way to do it?

盈利模式探讨 中国用户不肯花钱买App 开发者该怎么办

My code is available on this link :-

Drawing route on Google Maps using Google Maps Android API v2

I am developing pure location based app which includes map, speed and other related things which are related to Locations.

If anyone has any idea please kindly help me on resolving the issue of Speed and Distance.

解决方案

I had to deal with same problem, what you can do is to use Location Strategies code.

Then, on each update of location, you save the time of the current update. So, you will have the previous and current location, and time of update.

Then you calculate the distance in meters between those two locations (the old and new one)

private static long calculateDistance(double lat1, double lng1, double lat2, double lng2) {
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    long distanceInMeters = Math.round(6371000 * c);
    return distanceInMeters;
}

So, then you have the distance and the time difference, I think it wouldn't be a big deal to get the speed.

 
精彩推荐