Calculate speed between two location points in Android

做~自己de王妃 提交于 2019-12-22 18:25:04

问题


This seems like it should be simple enough, but I can't seem to get a clear example of how to calculate my speed using Android's Location Services.

All I want to do is calculate my speed based on my previous and current GPS position. getSpeed() doesn't work, because it needs to be set at some point using setSpeed().

So is there any sample code out there that I can run in Eclipse against my Android emulator running a GPX file, that will show me my pace in any measure (meters, feet, miles, etc., doesn't matter - I can always convert this to what I need).

Thanks in advance, Alex


回答1:


getSpeed() doesn't work, because it needs to be set at some point using setSpeed().

(In case you are using an actual device -- ). No, the speed is determined by the GPS chipset and it should be more accurate than getting the distance and time between two consecutive locations. You do not need to setSpeed() . Call hasSpeed() to see if the speed is valid.


(In case you're not using an actual device) I've had good success using the mock location provider where you can set the Location object you want with setSpeed() in meters/second

Here are some ways of doing this :

  • Mocking using NMEA
  • Mocking using code



回答2:


Do you just mean something like this? Gives you the speed in m/s.

oldLocation.distanceTo(newLocation) / (newLocation.getTime() - oldLocation.getTime())



来源:https://stackoverflow.com/questions/8202875/calculate-speed-between-two-location-points-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!