In my android application i need to GetSpeed from GPS
But the onLocationChanged() is called only every second even if set distance distance/time to 0 in requestLocationUpdates
How can i get the GPS Speed with higher frequency ? I don't need position, only speed
Is it possible to call GetSpeed outside onLocationChanged()?
Thank You very much
onLocationChanged is slow because it takes some time to fire get a first gps fix from the hardware. The speed estimate is a property of gps fixes. Hence speed changes along with with the location. You can try getLastKnownLocation() wich provides the last fix immediately. Use location.getTime() to decide if the last location is fresh enough.
GPS technology does not measure speed. It is merely a way to get accurate position. The speed value is based on position calculation.
That's why you can't get better frequency for speed values: the information is not there. Only (very short time) after a location update can there be any speed update.
Related
I have used onLocationChanged method of LocationListener for detecting change in location of my device. In requestLocationUpdates method I have set minimum Time = 5 seconds and minimum Distance = 2 meters, but requestLocationUpdates method is giving me updates even when my device is not moved at all (placed stationary). So please tell what is the issue with my code?
This my code:
public class LocationDetector implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Log.d("GPS: ",location.getLatitude()+", "+location.getLongitude());
}
.
LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
.
manager.requestLocationUpdates("gps", 5000, 2, new LocationDetector());
The GPS location can fluctuate. If you actually log the locations that you receive, you'll probably see that the location actually changes by 2 meters or more whenever a location update comes.
Edit: Ideas for dealing with the fluctuation added:
How to deal with GPS fluctuation depends on your application's needs. Some possible ideas include:
If you don't need a really accurate location, then use a higher distance limit in the requestLocationUpdates() call to not receive updates for very small location changes. You can think what's the absolutely necessary accuracy required by your use case and then use the highest possible distance limit.
If you don't expect the location to change very quickly or you don't need to react to location changes very quickly, then use a higher time limit in the requestLocationUpdates() call. This also makes sense if you have some very heavy code triggered by onLocationChanged() like if you always fetch some data over the network (reverse geocoding etc.).
The time limit also has more impact on the battery usage. Android documentation says:
...it is more difficult for location providers to save power using the
minDistance parameter, so minTime should be the primary tool to
conserving battery life.
If you really need an accurate location then there are some ways to decrease the fluctuation.
First of all the Location object received in onLocationChanged() usually has an estimated accuracy available by calling the Location.getAccuracy() method. You can simply ignore any location updates that have very poor accuracy (compared to the accuracies of previous location updates).
You can also do some filtering if you have a short buffer of the most recent locations. Calculating an average will reduce the amount of sudden changes but it also increases the response time. That is: a rapid change in the location will completely show up in the averaged location data only after some time. (Of course the averaged data starts to move towards the location right away, but it takes a while.) Also it will "let the spikes trough" to some amount.
If a fast response time is important and any major "spikes" in the data should be eliminated, then calculating the median is a better option. It will not smooth out the small changes in the data that much, but random spikes can be filtered out. If there's a real (permanent) sudden change in location, then the median filtering reacts to that with only a very small delay.
(These things are easy to try out in your favourite spreadsheet application.)
In application I need to get geo position of phone every x minutes (for now it's 5min and 50m), in such a way that phone doesn't discharge during day. The accuracy of position must be at least 40m. By now I have listeners for gps and network. The problem lies in the fact that the data I can receive sometimes is totally wrong (depends on phone) - new position can be even 200m from my real position and with 40m accuracy; next point can be the same but in opposite direction. For my app it's fatally.
So the question is next: Is there any way to get more accuracy data but not very often?
Update 1: to show the problem in action:
(The phone is lying in the fixed position)
To get more accurate location make sure you're using the ACCESS_FINE_LOCATION permission in your manifest. To prevent higher battery usage, you can use setInterval and setFastestInterval on your LocationRequest object. Longer intervals mean less battery usage by your app.
You should not be accessing the gps and network directly, you should be using LocationManager and using ACCURACY_FINE in your Criteria. This will help conserve battery life, and avoid issues with phones where the gps may have been disabled. There's more detail on that here.
As far as the accuracy of your location, you can use Location's getAccuracy method to get Android's estimate of how accurate a measurement is. If it doesn't fit your criteria, discard it and poll again.
How would I go about tracking the distance a user travels? I don't necessarily care about storing waypoints, although that may be necessary to calculate distance anyway.
So far, I am creating a LocationManager and regsitering an onLocationChanged listener, and then calculating the raw distance between two points when the listener fires.
The problem here is that if I leave the app sit still on a desk for 10, 15 minutes, it will say I have traveled .5 miles. How should I go about checking for accuracy and determining which points to use?
Or, even better - is there an SDK or .jar I can just include in my project and call their functions to make this whole thing easier?
Thank you for your time.
I am not an Android programmer but have implemented this functionality in my own embedded processor with far less processing capability than anything that runs Andriod.
This is nothing to do with the accuracy of a fix as determined by the GPS. If you have a stationary GPS receiver with "perfect" visibility of the sky and plot its reported position over time then you will see it wander around the true location, moving some metres an any direction. If you accumulate the distance travelled, in very small steps, around the true position you will end up with the distance travelled apparently taking you to the moon and back.
You need to set a movement threshold that is greater than the position accuracy of your GPS fix, and then only add in the distance travelled when you are sure that you have moved from the point at which you added in the last movement step.
You can call getAccuracy() on the Location object it gives you to check the accuracy. If your app needs to be real accurate, you can only count values with a high accuracy.
You can also call getProviders() on the LocationManager to check to see if you're getting coordinates from wifi, gps, or both, and ask the user to use their gps if it's not currently turned on so that you get more accurate points.
I made a simple GPS. app. for android, storing the route coordinates into file.
I'm confused, I got more onLocationChanged event when I stand in one place. The bearing and speed was zero of course in the Location when the event comes, but it Is interested, because I used 1 meter for minDistance when I registered the LocationListener. (the minTime was zero)
Are you sure that the values returned by getLatitude and getLongitude are identical to their previous values? Normal GPS is only accurate to within a meter or two, so it seems to me that random shifting of your location as perceived by your GPS sensor could be the problem.
You may be registering with more GPS satellites as your app runs and this may shift your position and reduce the error in all the times received from all the gps signals. Also, as you stand still position will become more and more accurate it is possible to get a gps signal accuracy down to cms see: http://atarist.home.xs4all.nl/geo/gps_accuracy.htm and wiki link on real time kinematic especially if you get a connection to a reference signal
I need to check the accuracy of GPS... but when i use location.getAccuracy then it doesn not return me any value... Does it take time..If it takes time then how long does it takes?
In android devices, mainly you can have two sources by which you can get position info : GPS_PROVIDER and NETWORK_PROVIDER
GPS_PROVIDER uses your GPS chipset to get a position fix. This has a greater accuracy.
NETWORK_PROVIDER uses the information from cell-tower to triangulate your location. Hence its accuracy is not as quite good.
You can get your accuracy info from Location.getAccuracy() or from NMEA data.
Does it take time..If it takes time then how long does it takes
Once you've started getting fixes it won't take much time. Time to first fix can be 30 seconds to 15 minutes. It depends on your GPS chipset, terrain conditions, cold/warm start etc.
To choose the best provider read this article