If GPS cannot fetch location, switch to network in Android - android

I am trying to get location based on GPS or network but the problem is that if I take based on GPS location it takes lot of time to fetch the location details. Hence I would like implement a method where if GPS signal is very low fetch location using network provider. How do I determine the GPS signal? if do there is problem with this? GPS signal is always low even in open sky and hence it would always get to network provider?
How do I fix this problem?

Are you polling the location with LocationManager.GetLastKnownLocation, or are you getting updates asynchronously via requestLocationUpdates?
For the first, query for the location with the GPS_PROVIDER. If it returns null, then query for NETWORK_PROVIDER
If you're getting notifications, request both. Then you can implement onStatusChanged to tell when the GPS is available or not, and ignore network updates if it is, or use them if not (the location object tells you what the provider was).

Adding lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); it will start fetching the gps using networ. Using location.getProvider() you should be able to differentiate to know whether its gps or network.

Related

Android NETWORK_PROVIDER locationManager

In my app I am using location services to obtain the user location coordinates to compare the distance to the objects which are obtained from my server.
So the user must have internet connection ON due to data obtained from server.
So in that case, I decided not to use GPS_PROVIDER, but only NETWORK_PROVIDER for user location, as the user needs to have internet connection, and this is quicker as GPS.
isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Till now I checked if isNetwork exists then do the stuff for NETWORK_PROVIDER, else try GPS_PROVIDER.
Now the question is, if the internet connection is always on, do I still need also GPS_PROVIDER?
I just want to get rid of the part of code for GPS, so I want to be sure, if the user has internet connection ON, the NETWORK_PROVIDER will always work.
To shorten the question: does NETWORK_PROVIDER depend from active internet connection? Does the user need to enable the device location in Settings for using just NETWORK_PROVIDER and not GPS?

Get Location updates from GPS only, not from Network

I need accurate location updates. When using FusedLocationServices from GooglePlayServices, even if I set HighAccuracy, it will get locations from Wi-Fi and cellular when GPS is not available. How do I request locations ONLY from GPS, and with no updates coming in when GPS is not available?
Use LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ...)
this removed the need for GooglePlayServices.Location completely.

Android GPS location isn't as accurate as network location

I've got an app which sets up two location listeners (one for GPS and one for network) and then chooses the location from the best one available, which should be GPS. However, if GPS is turned on, it will always choose the GPS location, even if the person starts the app up indoors. This has led to the GPS location being used even if the last known GPS location is from miles away and the network location is actually much more accurate.
Is there any way around this issue or is it just something that will need to be accepted as an issue with using GPS & network? Is the standard practice just to assume that GPS is always more accurate even though it's possible that it might not be in certain instances?
If you are using the network provider to fetch the location they are not very accurate when compared to GPS. So it can be a good idea to fetch the accuracy of your position using network provider when you are indoors as GPS doesnt function well indoors and you can use GPS to get the location updates when you are outdoor.
You can use GetAccuracy() method of Location class and see for the value it returns. Let Say if getAccuracy() retruns 25 then you can leave this value wait for another gps value until you get the desired value. Remember the value it returns is the radius of the circle in meters.

LocationService not retrieving location using only Network

I have re-read the whole documentation and as I understand, this API intelligently fuses the providers to give you an accurate result.
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs.
To my understanding, it will use Network and WiFi and GPS to give you a location. Now it is only logical to assume if one of those 3 is unavailable, it will use the remaining 2.
My problem is I'm testing my app to get Location when both WiFi and GPS are off. That will leave Location Service with the network provider only. But I'm getting Latitude and Longitude = 0.0
Long story short, either way I'm wrong in assuming that it will work relying only on network (no GPS, no WiFi) or there is something else I don't know or haven't read yet. Which of this 2 is my issue?
PS: my LocationRequest configuration is set to setInterval(30000) to setFastestInterval(60000) (30 and 60 seconds respectively, for each update.

getLastKnownLocation() Android, when the Last Known Location is updated?

getBestKnownLocation Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
when the Last Known Location updated in Android? is it updated if when there's an application listening for the location provider, if so what if there's no application that it ask for the location and then you asked for LastKnownLocation() ?
I thinki LastKnownLocation() is updating when some programs in your phone use this function requestLocationUpdates.
Please see Start location strategy
I did some investigations: I turned on GPS and waited to get a fix. Then I turned GPS off and drove 50km (31 miles). Then I used the code from A Deep Dive Into Location to get all the getLastKnownLocation. I tried it twice, first with GPS turned off and second with GPS turned on, but without a fix:
1) with GPS turned off I got
- Provider: network, correct location with accuracy 680m
- Provider: passive (mProvider=network), same location as above, same time as above
- Provider: gps, location null
So I learned that when gps is turned off you get no getLastKnownLocation from the GPS location provider.
2) with GPS turned on I got
- Provider: network, correct location with accuracy 652m
- Provider: passive (mProvider=network), same location as above, same time as above
- Provider: gps, location as it was 2h earlier with accuracy 12m, time was also 2h earlier
Here I learned that old messages are not invalidated, even it is obvious that they are wrong.
So to sum it up: when a provider is active, it stores the last received location retreivable via getLastKnownLocation. If the provider is deactivated, you don't even get getLastKnownLocation. Please note that I tested this with the GPS-Provider, other providers may react in a different way.

Categories

Resources