Working with locations is one of the most common tasks in the development process of mobile apps. Therefore, questions about what to use to achieve more precise results, maximum stability in operation and simplicity of implementation frequently arise during the development. In this article, we’ll look at Android OS and its tools: LocationManager and Google Location API Services.

 

LocationManager

 

As stated in the official documentationthis class allows you to use the services for a location, periodically update the geographic position of the device or send “Intent” to apps, in order to say that the device is near the predetermined location. To start using LocationManager, you need to get an instance of the class through getSystemService:

 

Context.getSystemService(Context.LOCATION_SERVICE)

 

It is worth noting that all methods Location API require ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION authorization.

 

Google Location API Services

 

It is fully inconsistent with LocationManager, but as for the real-time functioning there are some fundamental differences.

 

Implementation of the LocationManager

 

Let’s consider the difference in the implementation, using the example of receiving regular updates of current geographical position.

Our class should implement LocationListener for updates.

We need just 3 methods:

  1. The method with which the initialization of the LocationManager will take place while starting the service:
    private void init() {
       mLocationManager = (LocationManager)
       getSystemService(Context.LOCATION_SERVICE);
       }
  2. The method with which we will initialize LocationProvider:
    private String getLocationProvider() {
       final Criteria criteria = new Criteria();
       criteria.setAccuracy(Criteria.ACCURACY_FINE);
       criteria.setSpeedRequired(true);
       criteria.setAltitudeRequired(false);
       criteria.setBearingRequired(false);
       criteria.setCostAllowed(true);
       criteria.setPowerRequirement(Criteria.POWER_LOW);
       return mLocationManager.getBestProvider(criteria, true);
       }
  3. The method that will ask for the regular update of the location:
    private void startGettingLocationUpdates() {
       mLocationManager.requestLocationUpdates(getLocationProvider(),
       GPS_UPDATE_TIME, 0, this);
       }

    

After performing these actions, we are ready to get location updates using the onLocationChanged (Location location) method:

 

@Override
public void onLocationChanged(Location location) {
   mCurrentLocation = location;
   }

 

Implementation of Google API Location Services

 

Firstly, we should implement GoogleApiClient. ConnectionCallbacks,  GoogleApiClient. On Connection Failed Listener and LocationListener.

Next, 3 methods should be written:  

  1. The method that will initialize and start Google API Client:
    private void buildGoogleApiClient() {
       mGoogleApiClient = new
       GoogleApiClient.Builder(getApplicationContext())
       .addConnectionCallbacks(this)
       .addOnConnectionFailedListener(this)
       .addApi(LocationServices.API)
       .build();
        mGoogleApiClient.connect();
       }
  2. The method that will initialize and return LocationRequest:
    private LocationRequest getLocationRequest() {
       mLocationRequest = new LocationRequest();
       mLocationRequest.setInterval(Constants.GPS_UPDATE_TIME);
       mLocationRequest.setSmallestDisplacement(Constants.SMALLEST_DISTANCE);
       mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
       }
  3. The method that will ask for the regular location update:
    private void startGettingLocations() {
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,         mLocationRequest, this);
       }

  

After performing the actions stated above, as in the case of LocationManager, we are ready to get location updates using the onLocationChanged(Location location) method.

 

Advantages and disadvantages

 

As for Google Location API Services, if we compare it with LocationManager, we noticed significantly increased performance and stability during use. For example, there was a common problem with LocationManager receiving previous locations or map updates, when parameters had been set for the minimum period of time and distance by which the device must be displaced for subsequent updates; one of the parameters was usually ignored. In Google Location API Services, this problem was not encountered. Also, Google Location Services consume less battery powder.

As a result, Google Location API Services performed better in all cases. However, there are also some disadvantages. To use Location Services, you should install Google Play Services.

Therefore, in devices with usual firmware that are not supported by Google or in which Google Services are unavailable, the application will not work. This is a very important point that must be taken into account. Research into this area uncovered the fact that most users do not have Google Play Services. For example, they are not available in China due to the national policy, and this is a significant part of the market that cannot be ignored. In this case, it is necessary to turn to LocationManager.

Google Location API Services is the best solution for working with maps. This API allows you to work with the most accurate coordinates and with minimal expenses in terms of mobile resources, as long as the user has installed Google Play Services; otherwise, the only solution is LocationManager.