Alex
Alex

Reputation: 1110

What does getSpeed() actually do inside of Android?

I am writing an app which uses the onLocationChanged(Location location) callback along with location.getSpeed() to get the speed at which the user is traveling. I am curious as to what actually occurs when getSpeed() is called. I note that location is just a parameter fed into the callback by Android, which leads me to wonder:

is getSpeed() simply pulling an already-calculated field from this object, or does calling getSpeed() calculate the value in some other way?

Upvotes: 1

Views: 893

Answers (2)

martin k.
martin k.

Reputation: 166

I'm also curious many times what is in the source code or how is it called. I tried to find the code of android.Location class and it seems I'm succesfull. Try to check out this page: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/location/java/android/location/Location.java The method 'getSpeed' is at the row number 627, but it tells only 'return mSpeed;', so you have to look to the other parts of the class

Upvotes: 2

user3598138
user3598138

Reputation:

Android's getSpeed() will just return the value that was set in setSpeed().

The best way to get speed is to use simple physics:

Speed = Distance/Time

The thing about GPS' is that there is a lot of variability and thus accuracy isn't always the best in terms of speed. You should use a filter to help smooth the data (The Kalman Filter is pretty popular for navigation data).

Good luck!

Upvotes: 0

Related Questions