Alex T
Alex T

Reputation: 765

Calculate speed between two location points in Android

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

Upvotes: 2

Views: 4296

Answers (2)

Reno
Reno

Reputation: 33792

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 :

Upvotes: 1

Philip Pearl
Philip Pearl

Reputation: 1533

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

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

Upvotes: 2

Related Questions