Android Location Based Services

As an app developer, you may be interested in using Android Location Based Services (LBS) to create location-aware applications that can provide users with valuable information about their current location. These services can also offer location-based services such as weather updates, directions, and nearby points of interest.

In this comprehensive guide, you will learn about the fundamental aspects of Android Location Based Services, how they work, and how to integrate them into your app development projects.

What are Android Location Based Services?

Mobile applications have become an integral part of our daily lives, and location-based services (LBS) have played a crucial role in making them more relevant and contextual.

Android Location Based Services are a set of APIs and tools that allow you to create location-aware applications. These services provide you and other developers with access to the device’s GPS, Wi-Fi, and and cellular network data to determine the user’s location and provide services such as location-based notifications, geotagging, directions, and more.

With this information, you can provide location-based services such as weather updates, directions, and nearby points of interest.

In the Android ecosystem, there are several APIs and frameworks available for implementing location-based services in an application. The Google Play Services Location API, Android Location API, and Android Geofencing API are some of the most commonly used APIs.

Implementing Location-Based Services on Android:

Implementing location-based services in Android involves several steps which are discussed below:

  1. First, you need to request the necessary location permissions from the user in order to implement a location-based service on Android. Different permissions may be required depending on the API used. The Google Play Services Location API, for example, requires the ACCESS_FINE_LOCATION permission.
  2. When permissions have been granted, the location providers must be configured. Specify the level of accuracy, as well as the appropriate location providers, such as GPS or Wi-Fi.
  3. Using the appropriate API, the application can access location data after the location providers have been configured. To access accurate location data, the Google Play Services Location API provides a fused location provider.
  4. The application can provide location-based services to the user once it has access to location data. This can include location-based notifications, geotagging, directions, and more.

Location Object

The Location object is a fundamental component in Android used to represent a geographic location.

It stores essential information such as latitude, longitude, altitude, and accuracy of the location.

Obtaining a Location object in Android can be achieved through the LocationManager or FusedLocationProviderClient.

Here are some of the methods available in the Location class:

Methods Overview
getLatitude() Returns the latitude of the location in degrees.
getLongitude() Returns the longitude of the location in degrees.
getAltitude() Returns the height of the location above sea level in meters.
getAccuracy() Returns the accuracy of the location in meters.
getProvider() This method returns the name of the provider that generated this location fix, such as GPS or network.
getTime() Returns the milliseconds since the epoch at which the location was determined.
setLatitude() Sets the latitude of the specified location.
setLongitude() Assigns the longitude of a location.
setAltitude() This method specifies the location’s altitude.
setAccuracy() Sets the accuracy of the location.
setProvider() Used to set the location provider.
setTime() Sets the location’s time since epoch in milliseconds.

Location Quality of Service Object

To provide accurate location data, the Android platform provides a range of location quality of service (QoS) settings.

QoS settings define the required accuracy, power consumption, and response time needed to obtain location data.

Setting the appropriate Location Quality of Service (QoS) is crucial for obtaining location data in an Android app.

Android provides Location Quality of Service (QoS) methods that enable developers to set the accuracy and power requirements for location updates.

These methods are designed to optimize location updates for improved user experience and battery life. Here are some of the commonly used Location QoS methods in Android:

Methods Overview
setPriority(int priority) This method sets the priority for the location updates. The priority can be set to one of the following values:

PRIORITY_HIGH_ACCURACY: Used for high accuracy location updates that consume a lot of power.

PRIORITY_BALANCED_POWER_ACCURACY: Used for balanced accuracy and power consumption.

PRIORITY_LOW_POWER: Used for low power location updates.

PRIORITY_NO_POWER: Used for passive location updates, where the app only receives location updates when other apps request them.

setInterval(long millis) The interval between location updates is set using this method. The value is specified in milliseconds.
setFastestInterval(long millis) This method determines the fastest interval between location updates. It is used in conjunction with setInterval(long millis) to determine how often location updates are sent.
setExpirationDuration(long durationMillis) Sets the duration for which location updates are valid. The updates will automatically stop after this period.
setNumUpdates(int numUpdates) Sets the number of location updates that the app should receive before stopping updates automatically.
setSmallestDisplacement(float meters) This method determines how far the device must move before its location is updated. It helps conserve power and reduce the number of unnecessary updates.

Get Current location

Here is an example of how to get the current location in an Android app using the FusedLocationProviderClient:

public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private FusedLocationProviderClient mFusedLocationClient;
private Location mCurrentLocation;

protected void onCreate(Bundle savedInstanceState) {

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

private void getCurrentLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
} else {
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
public void onSuccess(Location location) {
if (location != null) {
mCurrentLocation = location;
Log.d(TAG, "Latitude: " + mCurrentLocation.getLatitude() + ", Longitude: " + mCurrentLocation.getLongitude());

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

public void onConnected(@Nullable Bundle bundle) {}

public void onConnectionSuspended(int i) {}

public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}


In above example, we will initialize the FusedLocationProviderClient in our app’s onCreate method. Then, we will call the getCurrentLocation method to retrieve the current location of the device.

We first check if our app has the necessary permission to access the device’s location. If permission is granted, we use the getLastLocation method to retrieve the last known location of the device.

Once we have the location, we store it in the mCurrentLocation variable and log the latitude and longitude.

To use the FusedLocationProviderClient in our app, we will need to add the necessary dependencies to our app’s build.gradle file:

implementation ''

Additionally, we must request permission to access the device’s location in our app’s AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Displaying a Location Address

To display a location address in your app, you can make use of the Geocoder class, a part of the Android framework.

By using the Geocoder class, you can easily retrieve the address information associated with a given latitude and longitude.

Let’s take a look at an example that demonstrates how to use the Geocoder class to display a location address:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;

try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {

if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressLine = address.getAddressLine(0);
String city = address.getLocality();
String state = address.getAdminArea();
String country = address.getCountryName();
String postalCode = address.getPostalCode();
String knownName = address.getFeatureName();

In this example, we initialized the Geocoder class by passing the context and default Locale, and then called the getFromLocation method with the latitude, longitude, and maximum number of addresses to return.

If the Geocoder was able to retrieve the address information for the given location, it returned a List of Address objects. To retrieve the address information, we used various methods provided by the Address class such as street address, city, state/province, country, postal code, and feature name.

It is important to note that we checked that the List is not null and contains at least one Address object before retrieving the address information.

You have learned about the location-based services in Android and how to use them to retrieve location data. Keep yourself updated with similar content by subscribing to our newsletter.

We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *