Android GPS reading control - android

I have a question about the GPS reading control. I use this function:
requestLocationUpdates(String, minTime , minDistance, LocationListener)
If I put the minDistance=100, does it mean the GPS will read the coordinate in each 100m? Is it accurate? How does it work (how can it know if the distance from my first location to the second is 100m)?

It means it won't report a location to your listener unless it's at least 100m away from the last location it reported to you. This is useful if your app does something battery/disk/network intensive on every location update but doesn't need a very fine resolution. For example, if you want to notify the user every time they cross city lines, you don't need to run the check on their coordinates every 1 second when they've moved 6 inches; a value of 30 seconds / 100m would probably suffice.

Related

requestLocationUpdates() method is giving updates without change in location - Android

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.)

Android: Right approach for sensing the distance between current location and some point of interest?

I have a question regarding my programming approach for android - whether it's efficient or not. Just to give you an idea what I try to accomplish:
There are multiple points of interests on a map. If the user gets nearer to a PIT, a notification about the distance shall appear. There are let's say 3 notifications: At a distance of 1 mile, 0.5 miles and 0.1 mile. When the user is more or less exactly at the PIT, no more notifications are shown. The user is supposed to drive a car or another vehicle.
I'm currently using Geofences to detect whether the user has entered a radius of 1 mile. If so, I check the distance of his current location to the PIT. If it's about 0.5 or 0.1 miles away, a notification is fired. Therefore, I check the current location frequently (right now every 10 seconds). If the user is like at the PIT or rather very close, the Geofence is exited and done.
The code is working fine and this approach is the first thing that came to my mind while reading the Android Developer's tutorials etc. But is there another approach that is MORE EFFICIENT?
Although this is (currently) meeting my needs, I have two major concerns:
It seems to drain the battery quite a lot. Since the user is most likely to be in a car where he can charge his phone during usage, this might be less of a problem.
I read the number of PITs from a file on the SD card. In my example I have just a few PITs. But the user is able to modify the source data so that he can add as many as he wants to. Google says that only 100 active Geofences are supported per user. But what if my user has more than 100 PITs? Since I'm using a Geofence to sense whether I have to fire the first notification or not - this is a severe problem. So is there an alternative?
I have a few suggestions :
1) To lower the amount of gps fixes, you could calculate a sleep time. The idea is to do frequent gps fixes when near a 'border' and few when far away. This could be further enhanced by taking the speed parameter returned by the GPS into account.
If for instance the user is 20 miles from the nearest border while driving 20mph, you can easily wait 30 minutes before turning on the gps again, just to give a simple example.
2) I have run in to the same problem. My solution was simply to select only the POI's within X miles. This way ensuring never to risk hitting the limit of 100. When the user has moved (X-1) miles from his initial position, the geofences are rebuild using the new location.
If you think there is a high risk of having such a high density of points that the above strategy still might fall, I would consider making your own proximity checking entirely based on gps instead of geofences.

How to track distance via GPS on Android?

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.

Android When I stand in one place I got more locationChangeEvent

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

Android location updates interval

I am experimenting with Androids location updates. The requestLocationUpdates is responsible for providing the updates. With the following code:
locationManager.requestLocationUpdates(provider, 300000, 10, this);
I am only supposed to receive updates 5 minutes and 10 meters apart. But the updates just keep coming in seconds apart and even when I am sitting still.
GPS is the provider I am using.
I need to space the updates out. What can I try?
The 10m is too small - I would increase that. GPS accuracy isn't great, and so every time it senses a small difference you will get another location. I'd bump it up to 100m and I expect you will then get a sensible number of locations coming through.
If you do want it more specific, then you'll need to handle the volume as more accurate means more volume.
Hers what i'd do:
First of all I check what providers are enabled. Some may be disabled on the device, some may be disabled in application manifest.
If any provider is available I start location listeners and timeout timer. It's 20 seconds in my example, may not be enough for GPS so you can enlarge it.
If I get update from location listener I use the provided value. I stop listeners and timer.
If I don't get any updates and timer elapses I have to use last known values.
I grab last known values from available providers and choose the most recent of them.
The 10m is too small for a GPS reading, try 100m. You'll also have issues with power saving mode, and app battery optimisation on most Android phones.
Android 10 and above has very strict background location updates, and may kill apps that run in the background. Later versions will also remove apps that have not be used recently.

Categories

Resources