Retrieve Location Using Locus Library

October 08, 2021 Publish By : EXPERT APP DEVS 2 min read Viewed By : 606
retrieve location using locus library in android

Retrieving location update is a tedious one as we have to handle all the scenarios whether a user has enabled GPS or not, handling location permission in manifest, and getting accurate location. All these are very frustrating from the developer point of view. For fetching location we have to define 

  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION

And an additional one keeping android 10 in mind

  • android.permission.ACCESS_BACKGROUND_LOCATION

We have to handle location updates when some activity/fragment gets initialized and have to destroy it in onDestroy().
To overcome this boilerplate code, there is an awesome library Locus that will solve our problem in just 3 lines of code.

Features

Locus includes many such features that are time-saving for developers.

  1. Built using Kotlin DSL
  2. Supports Android 10 Location permission changes.
  3. Supports Location Resolution
  4. Easy Configuration
  5. Highly customizable
  6. Lifecycle aware

Locus is built using Kotlin DSL which makes it so beautiful to write and customize easily. Also, Locus uses LiveData internally to make its lifecycle aware so you don’t have to worry about your fragment/activity lifecycle anymore.

Installation

In the project-level build.gradle, paste the below code.

project level build gradle

Add this dependency to your build.gradle(app-level):
dependency to your build gradle
Latest_version = 3.2.0

Retrieving Continuous Location Update

By just adding the below chunk of code in either activity or fragment, we'll get location updates.
Retrieving Continuous Location Update

The following scenario is handled by default with this library

  1. Android Permission model with default relational texts
  2. For getting high-resolution location updates, Location settings have been managed.
  3. Lifecycle aware out of the box.

Retrieving Location Update at Once

Add below the line of code when you want to retrieve location update only once.
Retrieving Location Update at Once

Stop Receiving Location Update

Stopping location updates is quite easy and clean. Just add the below line of code and done.
Stop Receiving Location Update

Handling Denials Scenario

Locus notifies you when a user denies location access or location settings resolution. When you receive an error from the locus, the error object can be used to check for these denials.
Handling Denials Scenario

Conclusion

Locus makes a developers’ life very easy when it comes to retrieving location. It is highly customizable and will fulfill most of the requirements that a developer needs.
 

Need a consultation?

Drop us a line! We are here to answer your questions 24/7.