FusedLocationProviderApi, PRIORITY_BALANCED_POWER_ACCURACY and GPS usage - android

I'm creating an app which tracks the location through FusedLocationProviderApi in a Foreground Service.
When I create LocationRequest with PRIORITY_BALANCED_POWER_ACCURACY and start listening for location updates, to my surprise GPS fires up ( Location in the phone settings is set to High Accuracy). I get the location almost immediately (through wifi/cell sources I guess), but as I'm indoors I can't get a GPS fix and it just keeps on trying without end...
Even though I didn't ask for PRIORITY_HIGH_ACCURACY I get hit with significant battery drain and get blamed for using GPS for several minutes.
Is there a way to set a timeout for GPS search (detect indoors situation)?
Is it possible to get a decent location using FusedLocationProviderApi without automatically triggering GPS or do I have to revert to the old location API?
Do I have to resort to limiting each LocationRequest to couple of minutes (setExpirationDuration) and create a new one to prevent the GPS battery drain?
[EDIT]: I think I might have found an another culprit. On a map tab in the app I have set setMyLocationEnabled(true), and this seems to be causing the GPS to flare up. I will experiment a bit more and post the results. Still I'm interested in how the location manager decides that enough is enough when it comes to GPS fix attempt.
Thanks in advance!

Related

Google FusedProvider does not call onLocationChanged() in an environment without GPS

My app works fine if the area has GPS (such as outside) or in houses/first or second floor buildings but it does not receive a location update if the area is something like the 4th-10th floor of a tall building, is this expected behavior or is this a limitation of location services?
Even with an internet connection on a high floor it still does not receive an update, this makes me believe that it is not falling back to Wi-Fi/mobile networks but I tried setting my Location method to Wi-Fi/Mobile only in the Android settings but it still works in normal environments.
Can anyone give me advice on what is happening? Do location services simply not work at higher altitudes or certain areas with very poor reception? I have not tested it in underground parking areas though.
I am using PRIORITY_HIGH_ACCURACY as my priority.
I am using PRIORITY_HIGH_ACCURACY as my priority.
That's the reason you are not getting Location change inside a building. Because HIGH_ACCURACY gets location from GPS provider. As we know GPS doesn't work well inside houses as it collects location from satellite.
You can try out other options.
PRIORITY_BALANCED_POWER_ACCURACY (About 100 meter)
PRIORITY_LOW_POWER (About 10km)
PRIORITY_NO_POWER (Act as a passive listener )
You may visit here if you havn't yet. Hope this helps.

Fused location API gives highly inaccurate location after closing google maps

Our application uses Google Fused location API to receive location updates every 3 minutes.
The location request settings are as follows –
Interval – 3min
Fastest interval – 1 min
Priority - PRIORITY_BALANCED_POWER_ACCURACY
In a normal running scenario, the location updates are received properly and the location accuracy is pretty good (around 20-25 m).
When the Maps application is opened, it received GPS location, and therefore our application also starts receiving location updates, which are basically from the GPS transceiver (I know this as the subsequent location updates have a speed and altitude value as well).
Problem occurs when the Maps application is now closed. Our application receives location updates, but the location coordinates do not change and every subsequent update received has a higher accuracy value. The updates may go up to even 1000m of accuracy and no accurate location are received. Opening the maps application again will make our application receive accurate and updated locations once again.
This problem can also be device specific as I saw this problem on a Motorola Moto G and not on my other Samsung or Karbonn phones.
In my experience, setting the priority to PRIORITY_BALANCED_POWER_ACCURACY never gets positions from the GPS, unless other app is using it. That's the same behaviour that you are finding in your app (in your case you only get GPS positioning when Maps is open), and that seems to be the general behaviour (see Does PRIORITY_BALANCED_POWER_ACCURACY exclude the GPS provider?).
I haven't found any official documentation to background this so I think that it may be device-dependant. In my tests, whenever I need GPS positioning I need to change the priority to PRIORITY_HIGH_ACCURACY.
Anyway, there is a known issue (57707) that is showing in some devices that causes the location provider to stop receiving position updates when set in PRIORITY_BALANCED_POWER_ACCURACY until the device is restarted.

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!

GPS is not working inside the building

After few hours of testing outside of house, when i came back to my house i found that GPS is enabled but was not getting location fixes inside the building.Hence its onLocationChanged method couldn't get called.
Problem: How to know that GPS is not getting any location fixes as device continue to sense your location in "trying mode".By trying mode i mean the situation where it is not coming to any result even after 20 to 30 minutes still it declared it self as enabled (blinking in status bar).
How one could know that the GPS doesn't get location so switch to another provider like Network_Provider.
In short i want to get my device to conclude something that GPS can find location fix for sure or you have to take location by another means.
I hope at least someone can give me idea about how to deal with that.
The link below has an awesome tutorial, of how to get the location from GPS and/or Network.
It uses a timer task, which analyzes if there is a GPS location in a specific period of time, assume 20 seconds. If not, it will return the location from Network as the current location. If there is a location from GPS, then it will compare which update is new (latest), and return that.
What is the simplest and most robust way to get the user's current location on Android?

Android LocationListener Switching from GPS to Network

I have a Service implementing LocationListener listening for both GPS and Network.
The application is dependant on a constant location-feed, but it seems when GPS has a hard time getting a locationfix network location doesnt step in.
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 30,0, LocationReporterService.this);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 2,0, LocationReporterService.this);
Questions
How do I make sure that I always get a location?
How can I make sure that if I dont get a GPS-location, I get a Network-location?
Is it a known bug?
Should I have 2 services, GPSLocationService and NetworkLocationsService?
Is there a solution to this? :)
I agree with most of comments from AlexBottoni good answer, although in some points I can't suppot him.
Overview
First, just to check that you are doing it right...
You setup the same LocationListener for both providers. To indentify from where you are reciving the location you need to test it like this:
public void onLocationChanged(Location fix) {
if(fix.getProvider().equals(LocationManager.GPS_PROVIDER)){
//here Gps
} else if(fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)){
//here Network
}
Also, you setup a different acquisition frequency. Gps is providing a new location every 30 seconds and Network every 2 minutes.
As you didn't impose a minimum distance, you should receive a new Location from each one of the providers (as long as they can get a fix) with the frequency requested. If you don't receive a fix, is because they weren't able to acquire one.
Also, it may takes a little longer then requested to get the fix (mainly with Gps), because it may take some time to shyncronize with satellites and fix a location.
Fallback
There is no builted-in fallback from one provider to the other. They are independet, as said by Alex. I'm using the following approach to implement fallback:
Register Gps listener and start a timer
On every GPS location, restart timer
If timer reachs end, register Network listener (Gps listener keeps registered)
If new Gps location arrives, unregister Network listener, restart timer
Preferable Provider
Although Gps may not be available everyhere, is far most precise then Network. In my town, I get 6 meters accuracy with GPS and 1 Km with Network :-(
Two services
Doesn't matter where you register the listener, activity or service, separate ot together, as long as you request them and the provider can get a fix, you will get the location (assuming no bugs in application :-))
Final Notes
Ensure you have the permissions need (ACCESS_FINE_LOCATION, INTERNET, etc).
Ensure that phone setup have Network Location enabled (usually default is disable)
Most Gps receivers support updating information about satellite location, which improves fix time. You can use GPS Satus from the market, to do it.
Regards.
This is really weird because, AFAIK, Android does not fall back to the second choice (network location provider) only if and when the first one (GPS) does not work. The two location providers are indipendent and should be sending location updates to you listeners indipendently to each other. Hence, the first one (GPS) should not be able to block the second one (network) in any case.
Regarding your questions:
How do I make sure that I always get a location?
You don't. There is no way to be sure. There are cases in which you cannot get any location fix just because no location reference is available. This is often the case in metro/underground stations. No GPS, no cellular antennas (not everybody lives in NYC or London...), no wi-fi hotspots so no way to determine your current position. Believe it or not, in the new hospital of my town, we have this situation at the moment because GPS constellation is not visible (indoor...), no wi-fi hotspots have been installed yet and the only available CellID antenna is just a few hundred meters away so you get the same signal everywhere, both outside and inside the building.
How can I make sure that if I dont get a GPS-location, I get a Network-location?
You cannot, either. It depends on the available networks. Anyway, you can check what you get from the location providers in your code and switch from one to the next until you get a usable location fix.
Is it a known bug?
No, it is not a bug. It is more a known limit of the existing technology.
Should I have 2 services, GPSLocationService and NetworkLocationsService?
It is something to try. As nick already said, this should not be a problem but a check should not hurt.
Is there a solution to this? :)
If your app is intended to be used mainly in a urban environment (a town), most likely you should use the network location engine as your main location engine. Nowadays, every village and town is covered by a large set on cellular phone antennas and by a large set of wi-fi hotspot so you are more likely to get a good location fix from the network location provider than from the GPS one. This is particularly true in towns with high buildings and narrow roads (that does not just mean NYC. Even here in Venice we have problems with the GPS). The network engine is also faster in getting a first fix and works indoor as well.
Fall back to GPS only if and when the network location engine does not work.

Categories

Resources