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

How to get speed in android app using Location or accelerometer or some other way(如何使用位置或加速度计或其他方式在 android 应用程序中获得速度)

本文介绍了如何使用位置或加速度计或其他方式在 android 应用程序中获得速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

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.

这篇关于如何使用位置或加速度计或其他方式在 android 应用程序中获得速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用位置或加速度计或其他方式在 android 应用程序中获得速度

基础教程推荐