How to calculate the average speed (Km/h)?

Asked

Viewed 2,413 times

1

I need to calculate the average speed (Km/h) and show this information in the Activity in "real time", as shown in the GPS Apps.

How to do such calculation and encode it on Android?

  • (Initial Distance - Current Distance)/(Initial Time - Final Time)?

  • Show, but how to expose it by Android? Need to use GPS coordinates?

  • Probably.

  • 2

    @Beet and OP: it is not simple. If the earth were flat it would be exactly as Beet indicated. Unfortunately the earth is a spheroid, so you have to use geodesic calculus. I sincerely hope that the Android localization API already has that calculation ready.

  • That’s what I thought too... rs, I believe it is not a simple calculation, but I also hope that Android return me something "ready".

  • 1

    The method Location.distanceTo() already help? It returns the distance in meters between two positions considering the Earth a ellipsoid WGS84.

Show 1 more comment

2 answers

5


0

If you really want to calculate speed on your own, go and study geodesic geometry. It’s more of a math problem than a programming problem. Or you can assume that, for small distances, the earth is flat. Remember that if you do this, the greater the distance you have travelled, the greater the error you will enter in the calculation.

There is an alternative with no work of horn better: use the method getSpeed of the Location class ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.