'Android : LocationManager vs Google Play Services

I want to build an app that centers around getting the user's current location and then find points of interest(such as bars,restaurants,etc) that are close to him/her via the Google Places API.

Upon searching the web for a place to start I came across some tutorials that use the LocationManager class and some others that use Google Play Services in order to find the users location.

On first sight both of them do the same thing, but since I am new to this I got a little confused and I don't know which method suits my needs the best. So, I want to ask you :

What are the differences between these two methods of finding locations (if there are any) ?



Solution 1:[1]

In my experience, "more appropriate accuracy" doesn't mean better by any means. Unless I'm missing something, if you want to make sure GPS is used, LocationManager is the only way to go. We track vehicles with our app and, again, unless I'm missing something, Google Play Services provides some very inaccurate locations quite often.

Solution 2:[2]

I have been using the Google Location Services API for quite a while. It does have advantages, since it encapsulates the complexity of having several sources for determining positions. However it encapsulates too heavily, so that when you get a strange position, you have no way to determine where that strange position came from.

In real life I have had several freak values pop up, being 10's of kilometers away from the actual position. The only explanation is that these crazy locations stem from errors in Googles Wi-Fi or NWK databases - errors which will always be there, since Wi-Fi and Network topologies change every day. But unfortunately (and surprisingly) the API gives you no information as to how an individual position was derived.

enter image description here

This leaves you with the problems of having to filter out freak values based on plausibility checking on speed, acceleration, bearing etc.

... or go back to the good old framework API and use GPS only, which is what I decided to do until Google improves the fused API.

Solution 3:[3]

You should use the Google Play Services location api instead of LocationManager. According to the docs:

The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.

As to why to switch, Google says this:

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy. It also handles location update scheduling based on power consumption parameters you provide. In most cases, you'll get better battery performance, as well as more appropriate accuracy, by using the Location Services API.

Solution 4:[4]

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy. It also handles location update scheduling based on power consumption parameters you provide. In most cases, you’ll get better battery performance, as well as more appropriate accuracy, by using the Location Services API.

More detailed diffferences between the two apis Google Play Service Location API and Android Framework Location API can be found here

Solution 5:[5]

Diffferences between the two apis Google Play Service Location API and Android Framework Location API based on GPS service

FusedLocationProviderClient

  1. For the 1st fetch, the location must not be null(i.e: Some other app need to update the Lastknown location in the GoogleplayService database. if it is null, need work around)
  2. For the next sequential Fetch, it uses requestLocationUpdates() method to fetch the location.
  3. The location fetch is only based on locationRequest.setInterval(milliseconds)and setFastestInterval(milliseconds), not based on user location change
  4. The LatLng value returned has contain only 7 decimal values (e.g: 11.9557996, 79.8234599), not as accurate
  5. Recommended, when your app requirement takes current location distance negligible of (50 - 100 meters accuracy)
  6. Effective in Battery usage.

LocationManager Api

  1. The user location fetch is invoked using locationManager.requestLocationUpdates()
  2. The location fetch based on user location change and time intervals locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, milliseconds, mindistance, Mylocationlistener)

  3. The LatLng value returned has contain 14 decimal values (e.g: 11.94574594963342 79.81166719458997) accurate location values

  4. Recommended for location based app, when needs more accuracy even in meters.
  5. Battery usage is based on fetch interval and fetch distance.

Solution 6:[6]

Yes, the Google Play Services Location Services API can give very misleading Location info. WiFi modems get moved, WiFi modems get updated with incorrect location information (ie if location is spoofed by an Android device that updates the WiFi modem's location) and there are a host of other circumstances that can result in incorrect Location data from WiFi modem triangulation. In all our apps where precise Location is mandatory, we use GPS only.

Solution 7:[7]

Android Location

LocationManager - Context.getSystemService(Context.LOCATION_SERVICE)

  • ACCESS_COARSE_LOCATION - provide less accurate location(city block) but faster and does not drain a battery so much

    • NETWORK_PROVIDER - uses cell towers, wifi access points
    • PASSIVE_PROVIDER - subscribes on location updates when somebody else in the system uses another providers
  • ACCESS_FINE_LOCATION - provides better and accurate location(up to few meters). Uses the same providers as ACCESS_COARSE_LOCATION

    • GPS_PROVIDER - satellites are used

Google API Location Services - GoogleApiClient based on Google Play Services. High level API with access to location events which go through the system. It has better battery performance but could not be installed on some devices

  • Fused Location Provider - automatically select an appropriate provider based on your needs and device conditions

Solution 8:[8]

Adding to the accepted answer, when enabling GPS through AlertDialog solution provided by Google. The implementation with ActivityResultContract is as follows:

// Global Activity Scope

ActivityResultLauncher<IntentSenderRequest> gpsRequestLauncher = registerForActivityResult(
        new ActivityResultContracts.StartIntentSenderForResult(), callback -> {
        if(callback.getResultCode() == RESULT_CANCELED) {
            // Request Cancelled
        } else {
            //  GPS Enabled
        }
});

Since the code from documentation is outdated with deprecation of onActivityResults

task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    IntentSenderRequest request = new IntentSenderRequest.Builder(
                        e.resolution).build();
                    gpsRequestLauncher.launch(request);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                }
            }
        }
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kit
Solution 2
Solution 3 NoChinDeluxe
Solution 4 Dhruvam Gupta
Solution 5 Sackurise
Solution 6 user1608385
Solution 7
Solution 8 Mirwise Khan