What does getSpeed() actually do inside of Android? - 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?

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

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!

Related

Can I get hold of the original Geofence location from that Geofence?

I'm writing an app using Geofences but I would like a GeofencingEvent to return the centre of the triggered Geofence (in other words, the latitude and longitude given to the Geofence builder when setting it up, rather than the location that actually 'triggers' the Geofence).
I realise I can keep a list of the Location I use for each Geofence and cross reference, but I'm a perfectionist and that method seems a bit dirty, complicated and overblown.
Looking at the Geofence code, I note that the Geofence.Builder internally returns a ParcelableGeofence. I know that I can cast the Geofence returned by getTriggeringGeofences() into a ParcelableGeofence and see what I believe to be the Geofence latitude and longitude in the agW and agX properties. This doesn't appear to be documented stuff so I'd rather not be using it in an app as it might change at any time (if the Location list is a bit dirty then this method this is utter filth!).
So my question is.... has anyone else either implemented their own version of the Geofence interface or extended the concrete ParcelableGeofence in order to achieve something similar and more elegant than my proposed hacks?

Does the getSpeed() in Location object account for Altitude change

I was wondering for one of the apps I'm writing does the Location.getSpeed() account for Altitude change? i.e. if I am going down a slope does it return a speed as if I'm going straight or does it actual do the calculation based on the triangle formed due to the downward slope?
The Location class does NOT consider altitude when giving you the speed.
The source for getSpeed is
public float getSpeed() {
return mSpeed;
}
And mSpeed assignment is mSpeed = (Parcel)in.readFloat();. Ultimately, if I'm not mistaken, it looks like the Parcel comes from ILocationManager.aidl which is private so I don't know, but I would guess it pretends you're not on a slope when it decides what the speed is.
And a lot less fun, but the docs say about getSpeed():
Get the speed if it is available, in meters/second over ground
which sounds like it does not consider altitude changes.
If you really want to know, you could somehow test it or you could file a bug regarding the getSpeed() documentation.

Google direction and voice navigation

I have been working on a android project.
Then I came up with 2 question.
Q1. how to implement navigation drive ?
My logic and some work
- I am be able to draw path between 2 address. And my thought is that, use the onLocationChanged(current) method then call https://maps.googleapis.com/maps/api/directions/output?parameters with the current location and destination which through some method to draw the path on the map.
Upon every onLocationChanged() method call, I redraw the path on the map again.
" Is it how we would do it for navigation ? "
Q2. how to implement voice navigation to work with Q1 ?
- Did some research, can't find anything that seems clearly helpful. All I know its that, in the return JSON from the /api/directions, there are direction instruction in it.
" Do I use it for voice from the return JSON ? Or there is a better way ? "
Would be very helpful with some link or example in details.
Thanks in adavnce
Here is what I know, hope it helps you out.
Regarding the first question:
After retrieving the directions and the necessary data, you have to draw the direction once and only once! yes, you have to use the onLocationChanged() but not to redraw the whole thing again.. if you notice in most of the navigation application they still keep the main route, they don't remove the passed parts... but you have to use onLocationChanged() to check if the user is out of the drawn path (by maybe 100m) so you have to re-calculate and redraw it again... redraw the path every time the user move is a costly operation it is better to be avoided...
For the second question:
As you said, the data retrieved already has the navigation commands.. so what you have to do is create a class to map the command with the voice.. and if you notice within the legs -> steps tags, there is a start and ending coordinates for each sub-path, so you can use these data to calculate the distance between them on each 200m say the command that "how far the user is to turn left" for example.
Hope this gives you a general idea of how it works. Good luck and happy programming.

Android: Best way to get instant location once per use

I always had some difficulties to find a good way for the instant location of the device.
What I want to do is once per use of the app (e.g. when the onCreate of an activity is called) i want to know the coordinates of the device in that exact moment and never ask for them again.
What I think could be the best way is to have something like a static Class with a function similar to :
coordinates getCoordinates();
Some advice/snippet to give?
Use LocationManager.getLastKnownLocation or LocationManager.requestSingleUpdate.
The first will return immediately, but may return null if no location is already available.
The second will return your data on a callback, but will wake up whatever provider is needed and will get a good location (if possible).
I have found what I really was searching in THIS answer.
I have only added two controls; i check if:
gps_loc.getTime() (or net_loc.getTime()) is bigger than System.currentTimeMillis() - 300000 (5 minutes ago). In the method run() of the inner class GetLastLocation;
location.getAccuracy() < 100. In the method onLocationChanged() of both LocationListeners (network and GPS).
Hope it helps!

Android gps how to skip distance?

I am working on a GPS-enabled application and I need to record a point each N meters. However, I can't see how I can use onLocationChanged() method in the LocationListener or any other method/class. The onLocationChanged() method gives a point each second, and I need to store each N-meter point.
I believe that this has a simple solution, but since I am beginner in Android, cant find it.
Any help will be much appreciated.
requestLocationUpdates has a minDistance parameter, that
if I recall correctly does what you want. I haven't been able to test this on a real phone though, so I don't know how accurate it is.
In onLocationChanged, compare the location you get with the last one you stored. If it's less than n meters, discard it. If not, store it. Rinse. Repeat.
EDIT: Wait, even easier - doesn't requestLocationUpdates have a minDistance parameter? See here: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates
it is working perfectly
myManager = ((LocationManager) ApplicationController.getAppContext().getSystemService( Context.LOCATION_SERVICE ));
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1 * 1000, 0.00001f, this);
mintime =1000ms always it is calling ....

Categories

Resources