Use GPS and Network Provider at the same time in Android
Asked Answered
L

3

22

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,

Lashawn answered 4/1, 2011 at 15:49 Comment(0)
R
27

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.

Regina answered 4/1, 2011 at 17:16 Comment(0)
H
2

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/

Hortenciahortensa answered 13/12, 2012 at 17:33 Comment(2)
Adding this as a comment of daren's answer would be more clearRupture
This really should have been a comment added to the answer from Daren.Sieracki
Q
0

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.

Quagga answered 25/9, 2022 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.