Faster GPS fix in Android - android

I have noticed that applications like GPS Status get a fix pretty quickly. When I try to get a fix within my own application it takes more time. Does anybody know why is this happening? Do they use a hidden part of the API to force the GPS to connect faster?
Here's what I use
LocationManager loc = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
listener);
I want the GPS to give results as frequent as possible because I need it for an AR app :)
Although I putting settings like 1000 ms and 1 m for the update frequency with no perceivable difference in speed of fix.

Could be one of several approaches:
Using LocationManager.getLastKnownLocation (which, given how many apps use location data, should generally be 'close enough' if not dead accurate.)
Using LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER to get an immediate rough fix, then refining with GPS (if available).
Running a service in the background that periodically gets the latest position and caches it (basically the same as LocationManager.getLastKnownLocation, but doing it yourself)

Here's my strategy : check this StackOverflow answer.

Related

How to most accurately determine the location of an Android device?

I'm building an app that should be able to report the users exact location. There is only a need for a single location, i.e. I don't need to track the device continuously.
I want the location to be as accurate as possible, and it's okay to wait a short while for the location to be determined (max 1-2 minutes).
I've been looking at FusedLocationProviderClient.getLastLocation(), but since I want the location to be as accurate and updated as possible it doesn't fit my needs.
So I started looking at using FusedLocationProviderClient.requestLocationUpdates() instead, and it seems like a better choice.
But I'm not sure how to best configure my LocationRequest to get as good accuracy as possible. For instance, would it be better to use setNumUpdates() so that I only receive a single update and use that as my location, or should I receive multiple updates in hopes of getting better accuracy (GPS locking to more satellites for example)? I'm thinking that if I use the second approach, I could look at the value of getAccuracy() from each location update and only keep the one with the highest accuracy. The downside is that if the device is moving and I keep receiving updates for a minute or so, the first location could have the highest accuracy, but since it's a minute old it's not accurate any more.
As stated above, I need just a single highly accurate location and it's okay for the app to wait 1-2 minutes for it if needed. What would be the best approach in this kind of scenario?
First, make sure the accurate location is turned on. look at Settings.Secure.LOCATION_MODE_HIGH_ACCURACY It has a noticeable advantage over only using GPS. Then listen for the location for a while and calculate the result you get to find out the best location. You can also detect if the user is moving if the number differs a lot or by using Activity Recognition API. It shouldn't be very hard to write this calculate function to get the best result.
I'm not sure about this but I really don't think waiting more than a few seconds gives you an advantage. to be sure you can simply alter this time and watch the result.
You might want to use LocationManager. In my experience FusedLocation will indeed appear to lock faster but may not be as accurate overall, or at least for a while. I have an app that also requires pretty accurate coordinates. My default is to use a LocationManager based approach but users can switch to a FusedLocation provider if they want faster locking (like when indoors).
This is a good overview https://developer.android.com/guide/topics/location/strategies
For the provider when requesting updates I'd use LocationManager.GPS_PROVIDER. It will take longer to lock since it will wait for satellites and not use Wifi or other towers. But you said that's OK. Something along these lines
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, YourLocationListener);
Once you start getting location updates via your listener's onLocationChanged() you can start to inspect the location for accuracy, movement from last location change, etc. to try and evaluate if the GPS receiver is still settling in on a location. Once you are satisfied with the quality of the fix you can stop receiving location updates (locationManager.removeUpdates(YourLocationListener)) and then run your logic that needs the location. The link I provided has good info about this too.

Polling satellite data from Android GPS in a battery efficient way

This is a very close question to lots of other questions out there (i.e. Android Regular GPS Polling in Service, maximizing battery life). However, what I need is quite different.
So, basically im looking for a way to regularly poll Android devices using the LocationManager class. Specifically, I want the SATELLITE information (NOT the Location itself). I.e. I just want the number of satellites, the signal-to-noise ratio and so on. I am looking to do this in the most battery efficient way possible.
One way to do it is to get the satellite information using the onGpsStatusChanged callback, and then getting the satellites with something like location_manager.getGpsStatus(null).getSatellites(). This is indeed, how I am doing it.
Relating to this now, I have a few questions that I cannot find anywhere in the Android documentations or in any forum online i've seen!
Will onGpsStatusChanged be called without adding a LocationListener to my LocationManager? Can I just somehow query the GPS receiver to send me the satellite info without registering a LocationListener as well? I.e. Is there a direct way of doing it other than something like:
location_manager.addGpsStatusListener(gps_status_listener_);
location_manager.requestLocationUpdates(location_manager.getProvider(LocationManager.GPS_PROVIDER).getName(), 3000, 2f, location_listener_);
Because for me, the LocationListener is useless.
Is it more battery efficient to attach the GPSStatusListener periodically, get satellite info and remove the listener OR just attach it once and keep polling until a certain period where I don't need it anymore. I realize continuous polling is battery inefficient and most of the questions out there deal with polling the LocationManager with requestLocationUpdates and setting minTime and minDistance etc. to minimize the amount of battery consumed. However, I only need the satellite information, so maybe there is a way to poll continuously without this consumption of battery?
This leads right into my next question which is: How quickly does the GPSStatusListener get the satellite info on average. I realize getting a LOCATION takes a while and depends on the Providers and LastKnownLocation and yada yada. But what about the satellite info? Is it quicker?
If anyone can help me out here, I would be highly obliged! Thank you in advance all!

Android Location Updates get stuck

I am developing an android application wherein I need the user location updates pretty frequently. Say 2 times a minute.
Earlier I had been using Google Play Service's "Fused location service" but the location updates were not received as requested.
The location updates got stuck for sometime, the interval between updates jumped to 10min or so.Sometimes even if I put my priority to "PRIORITY_HIGH_ACCURACY" the same happened.
I then went back to the old "Location Manager" and when I used the "NETWORK_PROVIDER", I noticed that the location updates got stuck due to this provider. Also the GPS does not get activated immediately, it takes some time. I am trying to build my custom fused location provider. How can I efficiently switch between providers, without getting lags on location updates.
I want to know what are the best practices for getting location updates regularly, all the time, be it either NW, GPS or both. Like it should work for an application where location updates getting stuck cannot be afforded.
Battery drain is not an issue for me right now.I am aware of all the supporting docs that Google provides regarding location access.
Any help would be much appreciated.
Thankyou !
FusedLocationProvider really is the best option for obtaining locations at the moment, because it uses a lot more than just GPS or Network data to obtain location fixes. I have experienced issues regarding intervals being missed as well, but ultimately this is something down to luck depending on availability of GPS, Network, etc. etc.
My favourite usage of FusedLocationProvider so far is in conjunction with the AlarmManager class: Basically, the idea is to start location tracking at intervals specified by the Alarm Manager (this can be set to every 30 seconds such as in your case). Once the interval is hit, the location provider is to obtain a location fix as soon as possible (so in the setInterval method or whatever it's called, the parameter is a 0). This way, you can avoid having to wait another 30 seconds for a new location, instead having the location tracker attempt to provide a location as soon as possible after the interval specified by the Alarm Manager is hit.
By the way, when making custom location tracking wrappers, be careful of using the .getLastKnownLocation() method as it only uses cached locations - you could end up sending the same location to the user every 30 seconds.
Good luck!

Use GPS and Network Provider at the same time in Android

I have implemented a locationlistener in my app which uses the Network Provider.
This all works fine because i want a location quickly and with GPS Provider it takes ages.
But I've come to a point in my app where location has to be accurate (max 5-10meters).
I was wondering if it's possible to use the GPS provider with the Network Provider at the same time and then get the best result of both?
And is it also possible to let me know when the GPS provider provided me with an GPS Location?
So basically:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Will this work with the same overridden onLocationChanged() ?
And how can I see if the GPS has gotten a Location?
Thanks,
You can certainly use the same listener for multiple providers. It may be better to use locationmanager.getProviders with a Criteria object then sort by accuracy or just listen to all of them. Not much practical difference though.
The onLocationChanged callback gives you a Location object, which has a getProvider() method you can use to determine where it came from. It also has a getAccuracy() method, so you could also sort your recent fixes by accuracy.
Ideally, if you are not concerned on the battery usage, then it perfectly fine to use both providers. As Daren explained, you can filters the coordinates by using getProvider() and getAccuracy().
http://blog.shinetech.com/2011/10/14/a-good-look-at-android-location-data/
Ideally, if u are using the same listener for multiple providers there are 2 issues to it.
1.) You are requesting location from multiple providers thus more continuous use Battery. - This can be mitigate depending on your use case. You can unregister the listener once u have received a good location (i.e. - not require a continuous location updates for long).
2.) Making your listener synchronized - And this is important, for code to be more stable - You do not know how these listeners would be called. If internally Android calculate the location from different provider on a different thread than ur listener maybe called on the same main thread from 2 different call points. Same listener object is called.

How am I notified if I can't get any gps fix location?

For example, I am inside a building and i want to get my location with the accuracy of 0.75 (Criteria.ACCURACY_FINE) and this will use the gps if my gps is on. Since I am inside a building, gps won't work.
How can I determine if it is really impossible to get a gps fix location?
Is onLocationChanged(Location arg0) will be called even though no gps fix location was received when using LocationListener?
Is it possible to use a timeout in requesting gps location so that I can shift to network as the location provider if i can't get any location?
I see three basic possibilities:
Setup a LocationListener, and then
determine it based upon calls to
onStatusChanged. My concern here is
that I'm not sure how well android
is able to determine the difference
between OUT_OF_SERVICE and
TEMPORARILY_UNAVAILABLE. I would
hope that its based upon signal
strength, but its probably something
to test.
Simply listen to both. If you haven't
heard from the (more accurate) gps provider
in a certain period of time, then switch to the
less reliable approaches. This may consume more power
due to the requirement to monitor multiple location sources
simultaneously, but is probably the most reliable method otherwise.
Some hybrid of 1 and 2 above (gps, but switch to local if no results for a while)... this might be the most efficient, but more difficult to implement.

Categories

Resources