16

Earlier to get user current location I have used LocationManager:

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

It is easy to read and very straightforward code.

But I have noticed that Google recently released New Client API Model in Google Play Services and suggests to use FusedLocationProviderApi which looks like much more complicated, it is async, it requires to handle callbacks etc.

Are there any advantages of using FusedLocationProviderApi over LocationManager?

Vadims Savjolovs
  • 2,618
  • 1
  • 26
  • 51
  • It provides better accuracy with less battery drain, as it switches between gps and WiFi based location. To avoid the complexity I recommend using this library: https://github.com/mcharmas/Android-ReactiveLocation – cYrixmorten May 09 '15 at 20:15

3 Answers3

16

FusedLocationProvider uses a mix of hardware to determine location based on the context of the request, meaning it's optimized transparently to you. It will also cache captured locations between applications to avoid unnecessary work to determine location info. So if a user has a variety of location-aware apps, they potentially avoid taxing the device (and waiting) for a location capture as one may have already been cached.

While the ol' LocationManager will suffice in small, one-off situations, you should definitely consider the newer alternative as the benefits may be great, and the work to implement, easy.

You may as well use it as Google Play Services is regularly updated across devices, and continuously includes improvements to location-based features (and more).

A link to an explanation of the FusedLocationProvider at launch: https://www.youtube.com/watch?v=Bte_GHuxUGc

Paul Cafardo
  • 226
  • 2
  • 6
4

In some situations FusedLocationProvider performs horribly. If you want to track your path on a map for example, it may decide to switch between GPS data and Wifi data. To calculate an accurate distance travelled, it is much better to force GPS readings with LocationManager, especially if you also want to track altitude, which wifi doesn't provide.

Carson Holzheimer
  • 2,890
  • 25
  • 36
  • In one of my project I need to capture user's location at every 10 secs interval. I used GPS and Network provider .My path is road near the river . When I plot captured lat long in google map it shows me 5-6mts diffrent lat long than actual lat long and which falls in the river . According to this testing senario final conclusion is user's path is on river which is wrong in relatity . So can I use fused in my situation . Right now all works fine using fused but as per your comment I have doubt so please suggest me . In my case I required proper lat long . – Gevaria Purva Sep 26 '16 at 06:15
  • In my testing, and by what I have read I think if you want to track a path accurately, it's better to force GPS with location manager. If battery life isn't a huge concern its the best. I've noticed that other high quality apps like S-Health or MapMyRun etc seem to use location manager, as their tracks look very similar to mine when I run these apps at the same time as mine – Carson Holzheimer Sep 26 '16 at 20:38
  • I used that also but my path is not accurate in that . I spend to many hours of time in testing and I tried different devices also . – Gevaria Purva Sep 27 '16 at 03:28
  • How are you comparing your coordinates to the real world? Remember Google maps or satellite may not be aligned correctly – Carson Holzheimer Sep 28 '16 at 12:42
  • Ok then it was my mistake that comparing my lat long with google map . Can you please tell me how to get actual lat long for comparison ? – Gevaria Purva Sep 29 '16 at 03:19
  • 2
    You can use google map, but just check that as you move around there isn't a consistent offset to your real location. – Carson Holzheimer Sep 29 '16 at 15:16
0

Just keep in mind that FusedLocationProvider uses LocationManager under the covers to at least get GPS points. It may be performing Wifi scans and doing a look up on the results to determine location as well, instead of using the Network provider, but there isn't any documentation about this that I am aware of.

Unless you cannot or do not want to use FusedLocationProvider, I would recommend using it.

Graham
  • 7,431
  • 18
  • 59
  • 84
Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36