LocationManager#requestLocationUpdates allows you to pass in a minTime and minDistance parameter. I'm having a hard time deciding what these numbers should be, and could appreciate some guidance.
Our app is not a turn-by-turn navigation app; I just want to show the 10 nearest points of interest. Since I'm showing the 10 nearest, they can get a little stale, but if the user is in a moving vehicle, I'd want/need to update them pretty frequently to avoid staleness.
I imagine a lot of people are in the same position of vague requirements: "I don't want the data to be too stale, but I don't want to waste battery." How can I turn these vague requirements into concrete numbers?
***<Edit>***
As of JellyBean, the criteria is (minTime & minDistance), so it has
to satisfy both to return a location.
***</Edit>***
Based on your problem, it sounds to me like the minTime parameter is irrelevant.
What you really need to worry about is minDistance, so that if a user is in a fast moving vehicle, it will keep up.
If a person is driving 60 MPH, they move about 27 meters per second.
Considering this critera... I would say to use:
minTime = 60000 // update every 60 seconds
minDistance = 90 // in a fast moving vehicle, it will update roughly once every 3 seconds
It has been described nicely in requestLocationUpdates docs
The frequency of notification or new
locations may be controlled using the
minTime and minDistance parameters. If
minTime is greater than 0, the
LocationManager could potentially rest
for minTime milliseconds between
location updates to conserve power. If
minDistance is greater than 0, a
location will only be broadcast if the
device moves by minDistance meters. To
obtain notifications as frequently as
possible, set both parameters to 0.
Background services should be careful
about setting a sufficiently high
minTime so that the device doesn't
consume too much power by keeping the
GPS or wireless radios on all the
time. In particular, values under
60000ms are not recommended.
I can suggest a better idea that, initially request updates with a little larger interval and get the locations. Now check if the distance between the consecutive locations is more than the minimum distance to distinguish the nearer locations, change your request update interval to lower value. Similarly if in this lower interval the distance you computed of much lower that indicates the user in not traveling through vehicle then update interval to larger value.
To update the interval you have to unregister previous listener and then re-regiser with new value.
For your problem at hand, do take a look at the PASSIVE_PROVIDER. Basically it will help you get updates when any other apps might request for them. So, you can use this in conjunction with other provider.
Related
locationManager.requestLocationUpdates(provider, 2500, 2, this);
You need to walk 2 meters in every 2.5seconds to request for new location?. Correct me if i am wrong. Thank you.
minTime – Wise choice of value for minTime will help you reduce
power consumption. What you need to keep in mind is that elapsed
time between location updates will never be less than minTime, but
it can be greater, because it is influenced by the implementation of
each given Provider and the update interval requested by other
running applications.
minDistance – Unlike minTime this parameter can be turned off by
setting it’s value to 0. However, if minDistance is set to a value
greater than 0, location provider will only send updates to your
application if location has changed at least by given distance. This
parameter is not a great power saver like minTime, but it should be
kept in mind although.
These two parameters work in AND relation, so in order to receive location update, both of their conditions need to be achieved (i.e. more then 5 seconds has passed, and the distance change is greater than 10 meters).
You can check this link:
https://blog.codecentric.de/en/2014/05/android-gps-positioning-location-strategies/
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.)
I'm using Fused Location Provider library in android. It is working perfectly fine. But I've an issue with it, it returns location updates in 5 sec minimum.
I've tried every thing like setting minimum updates time to 1 millisecond, and distance to 0.01 meter and Priority to PRIORITY_HIGH_ACCURACY
My code :
locationrequest.setInterval(1); // 1 milliseconds
locationrequest.setSmallestDisplacement(0.01f); // 0.01 meters locationrequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
But no use, still minimum time between two successive location updates is 5 seconds.
My Questions is : Is there any way to decrease location updates time to 5 milliSeconds ?
-> I need location updates for only 10 minutes, So no issue with the high battery consumption.
-> I need any way (possible): Is there any external hardware available, which connects via Bluetooth and send location updates upto that level ?
Edits:
Let me ask you a different question :
What can be the minimum possible time for location updates and how to achieve that ?
Let's say i want to track a car, which is moving with the speed of 400 KM/h, means 5 meter in about 50ms. So can you suggest any better way to track this car ?
The precision and accuracy of location-sensing hardware (GPS, AGPS, etc.) means that getting updates more frequently than every few seconds isn't likely to provide meaningful results. In fact, technology like the Fused Location Provider is likely to prioritize getting more accurate results rather than providing results every few hundred milliseconds.
In addition to that, the battery drain from getting updates multiple times a second is likely to be very significant.
All that said. The way to get every update that your location-sensing hardware is receiving is to set the location update interval and minimum displacement to zero, and to prioritize accuracy.
locationrequest.setInterval(0); // No delay.
// locationrequest.setSmallestDisplacement(0); // This is the default.
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Note that this will give you every update, but depending on the hardware limitations, and potentially the Fused Location Provider implementation, there's no guarantee this will be any faster than the 5s frequency you've found so far.
I'm creating an application (For educational purposes) which records the user's location every 30 minutes, and enables the user the view all the locations on the map. I don't want updates to be more frequent than 30 minutes, but they are.
This is how I call requestLocationUpdates:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 60 * 1000, 0, pe);
Now, the documentation clearly says:
The elapsed time between location updates will never be less than minTime
But I did see some answers here on SO stating differently (This answer, for example).
It seems like I'm getting updates whenever they are available from the GPS. The GPS icon never turns off and the updates rate becomes greater than 1 update/second. So I have 2 questions here:
The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?
Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?
The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?
From Jellybean onwards devices must observe the minTime parameter, so it does have a purpose (now).
Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?
Yes, use a Handler to request one update with requestSingleUpdate() every 30 minutes.
I've tackled this before in a previous question, let me know if the code in that answer helps and if you have any questions it doesn't address.
The second parameter, min distance difference in meters is set to zero, it causes constant updates.
Prefer using requestSingleUpdate within a timer+handler on a desired period in minutes.
I developed an application that fetches GPS location. I specified minTime as 20 seconds and minDistance as 1 meter in requestLocationUpdate method. But still i am getting location in 2-3 seconds interval and sometimes it takes more than one minute. Can i get location at particular time interval and within 1 minute?
Please Help.
Thanks in advance.
Can i get location at particular time interval and within 1 minute?
Not necessarily.
First, you set minDistance to 1 meter. Try using 0.
Second, you are limited by how frequently the GPS radio actually gets fixes, which will be based on environmental factors and is outside of your control.
Third, minTime is a hint, as the documentation explains. Hence, you may get fixes more frequently than it, or less frequently. For a minTime of 20 seconds, it is unlikely that Android will power down the GPS radio between fixes, and so I suspect that value is not helping you any.