How to get speed in android app using Location or accelerometer or some other way(如何使用位置或加速度计或其他方式在 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 应用程序中获得速度
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01